/*Example of class for addition of the two numbers*/
import java.awt.*;
import java.awt.event.*;
class as extends Frame implements ActionListener
{
TextField t1,t2,t3;
Button b1,b2,b3;
public as()
{
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("ADD");
b2=new Button("SUB");
b3=new Button("EXIT");
setLayout(new FlowLayout());
add(b1); add(t1); add(b2); add(t2); add(t3); add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public static void main(String args[])throws Exception
{
as a=new as();
a.setSize(200,300);
a.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
int val1=Integer.parseInt(t1.getText());
int val2=Integer.parseInt(t2.getText());
int res;
if(ae.getSource()==b1)
{
res=val1+val2;
t3.setText(" "+res);
}
if(ae.getSource()==b2)
{
res=val1-val2;
t3.setText(" "+res);
}
if(ae.getSource()==b3)
{
System.exit(0);
}
}
}
import java.awt.*;
import java.awt.event.*;
class as extends Frame implements ActionListener
{
TextField t1,t2,t3;
Button b1,b2,b3;
public as()
{
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("ADD");
b2=new Button("SUB");
b3=new Button("EXIT");
setLayout(new FlowLayout());
add(b1); add(t1); add(b2); add(t2); add(t3); add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public static void main(String args[])throws Exception
{
as a=new as();
a.setSize(200,300);
a.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
int val1=Integer.parseInt(t1.getText());
int val2=Integer.parseInt(t2.getText());
int res;
if(ae.getSource()==b1)
{
res=val1+val2;
t3.setText(" "+res);
}
if(ae.getSource()==b2)
{
res=val1-val2;
t3.setText(" "+res);
}
if(ae.getSource()==b3)
{
System.exit(0);
}
}
}
No comments:
Post a Comment