Addition of two numbers using Java AWT
-----------------------------------------------------------------------------------------import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Testing extends Frame implements ActionListener
{
Button b1,b2;
TextField t1,t2,t3;
Testing()
{
b1=new Button("sum");
add(b1);
b1.setBounds(60,50,60,35);
b1.addActionListener(this);
b2=new Button("Clear");
add(b2);
b2.setBounds(130,50,60,35);
b2.addActionListener(this);
t1=new TextField();
add(t1);
t1.setBounds(60,100,60,35);
t2=new TextField();
add(t2);
t2.setBounds(130,100,60,35);
t3=new TextField();
add(t3);
t3.setBounds(60,160,130,35);
t3.setEditable(false);
setLayout(null);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
int a,b,c;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
c=a+b;
t3.setText(String.valueOf(c));
}
else
{
t1.setText("");
t2.setText("");
t3.setText("");
}
}
public static void main(String[] args)
{
Testing obj=new Testing();
obj.setSize(600,400);
obj.setVisible(true);
}
}
0 Comments