Sunday, 6 January 2013

Button click and event handling with applet in java


package event1;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;


public class Event1 extends Applet implements  ActionListener {
   Button btn1,btn2;
   public void init()
    {
  setLayout(new FlowLayout());
   btn1=new Button("blue");
   btn2=new Button("green");
btn1.addActionListener(this);
btn2.addActionListener(this);
add(btn1);
add(btn2);
// setSize(400,400);
//setBounds(200,180,200,140);
//setVisible(true);
}
  public void actionPerformed(ActionEvent e)
   {
       if(e.getSource()==btn1)
       {
          setBackground(Color.BLUE);
       }
       else if(e.getSource()==btn2)
        setBackground(Color.GREEN);   
   }
}

No comments:

Post a Comment