รันโปรแกรมไม่ผ่าน Swing GUI : Java
 

Forum :: Java :: รันโปรแกรมไม่ผ่าน Swing GUI

ผิดตรงไหนคะ หาไม่เจอ โปรแกรมรันไม่ผ่าน



import javax.swing.*;

public class JavaApplication3 {
    JFrame f;
    JPanel p;
    
    JTextField t,t1;
 JButton b;
 JLabel l,l1,l2,l3;

 JavaApplication3(){
       l = new JLabel("Adition of two numbers",JLabel.CENTER);
          l1 = new JLabel("First number",JLabel.CENTER);
          l2 = new JLabel("Second number",JLabel.CENTER);
          p = new JPanel();
        t = new JTextField(5);
       t1 = new JTextField(5);
       b = new JButton("Add");
  }
  
  public void showFrame(){
   
   
            p.setLayout(new GridLayout(2,2));
         p.add(l1);
         p.add(t);
         p.add(l2);
         p.add(t1);
         
         
      Container c = f.getContentPane();
            c.add(l,BorderLayout.NORTH);
            c.add(b,BorderLayout.SOUTH);
         c.add(p,BorderLayout.CENTER);
         f.setSize(150,150);
         f.setVisible(true);
         b.addActionListener(new Handler());
  }
  
 

  
   class Handler implements ActionListener{
    
    public void actionPerformed(ActionEvent e){
          int a = Integer.parseInt(t.getText());
        int z = Integer.parseInt(t1.getText());
        int y =a+z;
      l3.setText("Sum is "+ y);
    }
   }
    public static void main(String[] args) {
  JavaApplication3 n = new JavaApplication3();
  n.showFrame();
}

   }

0 Comment

  • Have no comment.

Comment Forum

 
 
 
Share This Topic Login with Facebook