Java

Wednesday, 29 April 2015

Java program to demonstrate button events

Program:



/*<applet code="ButtonEvent.class" height=400 width=400>
</applet>*/
import java.awt.*;
import java.lang.*;
import java.applet.*;
public class ButtonEvent extends Applet implements ActionListerevent;
{
Button b1,b2,b3,b4,b5;
TextField t1,t2,t3;
string str;
int n1,n2,n3;
public void init()
{
b1=new Button("add");
b1.addActionListener(this);
add(b1);
b2=new Button("sub");
b2.addActionListener(this);
add(b2);
b3=new Button("mul");
b3.addActionListener(this);
add(b3);
b4=new Button("dn");
b4.addActionListener(this);
add(b4);
b5=new Button("mod");
b5.addActionListener(this);
add(b5);
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
add(t1);
add(t2);
add(t3);
}
public void actionperformed(ActionEvent e)
{
try
{
str=t1.gettext();
n1=Integer.parseInt(str);
str=t2.gettext();
n2=Integer.parseInt(str);
t3.setText(" ");
}
catch(Exception ae)
{
}
if(e.getsource()==b1)
{
n3=n1+n2;
str=string.valueof(n3);
t3.setText(str);
}
if(e.getsource()==b2)
{
n3=n1-n2;
str=string.valueof(n3);
t3.setText(str);
}
if(e.getsource()==b3)
{
n3=n1*n2;
str=string.valueof(n3);
t3.setText(str);
}
if(e.getsource()==b4)
{
n3=n1/n2;
str=string.valueof(n3);
t3.setText(str);
}
if(e.getsource()==b5)
{
n3=n1%n2;
str=string.valueof(n3);
t3.setText(str);
}
}
}

No comments:

Post a Comment