I write this code in one class but it doesn't show any elements ( button , labels ) all elements , but when I put it in two classes one the main and another one it works fine .
Here is the code :
package averageprogram; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AverageProgram extends JFrame{ private int num = 0; private String userCo = ""; private JButton[] buttons; private JButton btn = new JButton("Click"); private JButton btn1 = new JButton("Calculate Average"); private Container cp = getContentPane(); private JTextField userInput = new JTextField(30); private JLabel label = new JLabel("Please enter the number of Courses: "); private JTextField[] userInput1; private JTextField[] userInput2; private JLabel[] label1; private JLabel[] label2; private JLabel label3 = new JLabel(""); private JPanel panel = new JPanel(); private JPanel panel1 = new JPanel(); private int s1= 70; private int s2= 88; private int s3= 77; private double total = s1+ s2+ s3; public static void main(String[] args) { JFrame frame = new JFrame("frame"); frame.setSize(600, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("program"); } public AverageProgram() { cp.add(panel); cp.add(panel1,BorderLayout.SOUTH); panel.add(label); panel.setLayout(new FlowLayout()); panel.add(userInput); panel.add(btn); panel1.add(btn1); ButtonWatcher handler = new ButtonWatcher(); btn.addActionListener(handler); ButtonWatcher1 handler1 = new ButtonWatcher1(); btn1.addActionListener(handler1); } private class ButtonWatcher implements ActionListener { public void actionPerformed(ActionEvent a) { try { userCo = userInput.getText(); num = Integer.parseInt(userCo); label1 = new JLabel[num]; label2 = new JLabel[num]; userInput1 = new JTextField[num]; userInput2 = new JTextField[num]; } catch (Exception exception) { JOptionPane.showMessageDialog(null, "Error Input"); } for(int i = 0; i<num; i++) { label1[i] = new JLabel("Course Name " + i); userInput1[i] = new JTextField(10); userInput1[i].setHorizontalAlignment(JTextField.CENTER); label2[i] = new JLabel("Course Grade " + i); userInput2[i] = new JTextField(10); userInput2[i].setHorizontalAlignment(JTextField.CENTER); } for(int i=0; i < num; ++i) { panel.add(label1[i]); panel.add(userInput1[i]); panel.add(label2[i]); panel.add(userInput2[i]); } SwingUtilities.updateComponentTreeUI(cp); } } private class ButtonWatcher1 implements ActionListener { public void actionPerformed(ActionEvent a) { try { if (userInput.getText().isEmpty()) { String message = "You must enter a number of your courses first"; JOptionPane.showMessageDialog(new JFrame(), message, "Dialog", JOptionPane.ERROR_MESSAGE); } else { int numOfCourses = 3; double avg = 0.0; double temp = 0.0; for(int i = 0; i<num; i++) { numOfCourses++; temp = Double.parseDouble(userInput2[i].getText()); total = total + temp; } panel.add(label3); avg = total / numOfCourses; avg =Math.round(avg * 10.0) / 10.0; label3.setText("The result is : " + avg + " %" ); } } catch (Exception exception) { JOptionPane.showMessageDialog(null, "Error Input"); } } } }