Java program for event Handelling

Java program for event Handelling

import java.awt.*;
import java.awt.event.*;
class EventTest implements ActionListener
{Frame fr;
Button b1,b2,b3,b4;
TextField tf1,tf2,tf3;
Label lb1,lb2,lb3;

EventTest( )
{ fr = new Frame("myframe" );
fr.setLayout(null);
b1= new Button ("Add") ;
b2= new Button ("Sub") ;
b3= new Button ("Mul") ;
b4= new Button ("Div") ;
tf1 = new TextField() ;
tf2 = new TextField() ;
tf3= new TextField() ;
lb1 = new Label("no1") ;
lb2 = new Label("no2") ;
lb3 = new Label("res") ;
tf1.setBounds( 50,50,100,50) ;
tf2.setBounds( 50,120,100,50) ;
tf3.setBounds( 50,190,100,50) ;
lb1.setBounds(10,50,35,50);
lb2.setBounds(10,120,35,50);
lb3.setBounds(10,190,35,50);
b1.setBounds( 30,260,35,50);
b2.setBounds( 85,260,35,50);
b3.setBounds( 140,260,35,50);
b4.setBounds( 195,260,35,50);
fr.add( tf1) ;
fr.add( tf2) ;
fr.add( tf3) ;
fr.add( lb1) ;
fr.add( lb2) ;
fr.add( lb3) ;
fr.add( b1) ;
fr.add( b2) ;
fr.add( b3) ;
fr.add( b4) ;
b1.addActionListener( this );
b2.addActionListener( this );
b3.addActionListener( this );
b4.addActionListener( this );

fr.setSize(600,700);
fr.setVisible(true);
}
public void actionPerformed (ActionEvent e )
{
if ( e.getSource() == b1 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = (s1+s2) ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}

if ( e.getSource() == b2 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = s1-s2 ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}
if ( e.getSource() == b3 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = s1*s2 ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}

if ( e.getSource() == b4 )
{
String str1 = tf1.getText() ;
String str2 = tf2.getText() ;
int s1 = Integer.parseInt(str1) ;
int s2 = Integer.parseInt(str2) ;
int result = (s1/s2) ;
String str3 =" " + result ;
tf3.setText ( str3) ;
}
}

public static void main (String s[] )
{
new EventTest();
}

}

Read More
admin March 17, 2014 0 Comments