Button Action using Swings in java


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;   
public class Test123 extends JFrame implements ActionListener
{   
    JTextField t1,t2,t3;
    JButton b1,b2,b3,cl;
    int a,b,c;
Test123()
{   
   
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t1=new JTextField();
    add(t1);
    t1.setBounds(30,40,70,30);
   
    t2=new JTextField();
    add(t2);
    t2.setBounds(110,40,70,30);
   
    t3=new JTextField();
    add(t3);
    t3.setBounds(190,40,70,30);
    t3.setEditable(false);
   
    b1=new JButton("add");
    add(b1);
    b1.setBounds(30,70,70,30);
   
    b2=new JButton("diff");
    add(b2);
    b2.setBounds(110,70,70,30);
   
    b3=new JButton("mul");
    add(b3);
    b3.setBounds(190,70,70,30);
   
    cl=new JButton("Clear");
    add(cl);
    cl.setBounds(270,70,70,30);
   
     b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     cl.addActionListener(this);
   
     setLayout(null); 
 
}   


public void actionPerformed(ActionEvent e)
{
   a=Integer.parseInt(t1.getText());
        b=Integer.parseInt(t2.getText());
    if(e.getSource()==b1)
    {
        c=a+b;
        t3.setText(String.valueOf(c));
    }
    if(e.getSource()==b2)
    { 
        c=a-b;
        t3.setText(String.valueOf(c));
    }
   
 
}
 

public static void main(String[] args)
{   
   Test123 t1= new Test123();     
     
    t1.setSize(400,500);   
    t1.setVisible(true); 
    t1.setResizable(false);
}   

   

 
}   

0 Comments