Sunday, 6 January 2013

Addition of two number with applet and event handling


package applet2;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Applet2 extends Applet implements ActionListener{
 
   Label lbl1,lbl2,lbl3,lbl4;
   TextField txt1,txt2;
   Button btn;
   public void init()
   { setLayout(new GridLayout(4,2));
      setBackground(Color.GRAY);
       lbl1=new Label("enter first Number");
       lbl2=new Label("enter second Number");
       lbl3=new Label("Sum");
       lbl4=new Label();
       txt1=new TextField(20);
       txt2=new TextField(20);
       btn=new Button("Result");
        Component add1 = add(lbl1);
       Component add2=add(txt1);
     
      Component add3= add(lbl2);
       Component add4=add(txt2);
      Component add5= add(lbl3);
       Component add6=add(lbl4);
       Component add7=add(btn);
       btn.addActionListener(this);
   }
   public void actionPerformed(ActionEvent e)
   {
       int s1,s2,s3;
       if(e.getSource()==btn);
       {
         s1=Integer.parseInt(txt1.getText());
          s2=Integer.parseInt(txt2.getText());
          s3=s1+s2;
          lbl4.setText(Integer.toString(s3));
       }
   }
   
}

2 comments: