Eventhandling

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import java.awt.

*;

import java.applet.*;

import java.awt.event.*;

/*<Applet code="action" height=100 width=800></Applet>*/

public class action extends Applet implements ActionListener

Label l1,l2,l3;

Button b1,b2,b3,b4,b5;

TextField t1,t2,t3;

public void init()

l1=new Label("First Number:");

t1=new TextField(20);

l2=new Label("Second Number:");

t2=new TextField(20);

l3=new Label("Result:");

t3=new TextField(25);

b1=new Button("add");

b2=new Button("sub");

b3=new Button("mul");

b4=new Button("div");

b5=new Button("clear");

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

add(b1);
add(b2);

add(b3);

add(b4);

add(b5);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

public void actionPerformed(ActionEvent e)

int n1,n2;

n1=Integer.parseInt(t1.getText());

n2=Integer.parseInt(t2.getText());

if(e.getSource()==b1)

int sum=n1+n2;

t3.setText(""+sum);

else if(e.getSource()==b2)

int sub=n1-n2;

t3.setText(""+sub);

else if(e.getSource()==b3)

int mul=n1*n2;

t3.setText(""+mul);

}
else if(e.getSource()==b4)

int div=n1/n2;

t3.setText(""+div);

else if(e.getSource()==b5)

t1.setText("");

t2.setText("");

t3.setText("");

OUTPUT:

You might also like