Saturday, 9 September 2017

Applet program for button

import java.awt.*;
import java.awt.event.*;
 class but extends Frame implements ActionListener
{
       TextField tx;
        Button b1,b2;
   but(String s)
  {
       super(s);
      setLayout(null);//method of Container
       tx=new TextField(20);
       b1=new Button("red");
       b2=new Button("green");
       b1.addActionListener(this); 
       b2.addActionListener(this);   
       add(tx);  //method of Container
       add(b1);
       add(b2);
       tx.setBounds(100,150,150,30); // method of Component
       b1.setBounds(100,250,50,30);
       b2.setBounds(180,250,50,30);
     addWindowListener(new W());
   }
  class W extends WindowAdapter
  {
     public void windowClosing(WindowEvent e)
   {
      setVisible(false);
     dispose();
     System.exit(0);//to stop the process
    }
   public void windowOpened(WindowEvent ob)
   {
       setBackground(new Color(100,150,200));
  }
  public void windowIconified(WindowEvent ob)
  {
       setBackground(new Color(255,255,255)); 
  }
}
  public void actionPerformed(ActionEvent e)
 {
   //method of java.util.EventObject
     if(e.getSource()==b1)
{
     setBackground(Color.red);
     tx.setText("red");
}
    else if(e.getSource()==b2)
{
    setBackground(Color.green);
     tx.setText("green");
}
    }
 public static void main(String args[])
 {
     but b=new but("mybutton"); 
    b.setSize(400,400);
    b.setVisible(true);
 }   
    }

   

No comments:

Post a Comment

Featured post

Development Of JAVA Program