Pages

Applet:How to use button Action with TextField

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonAT"  height=250  width=250>
</applet>
*/
public class ButtonAT extends Applet implements ActionListener
{

 Button b1,b2,b3;
 TextField t1,t2,t3;
 String msg="";
 
  public void init()
  {

  setLayout(null);
  b1=new Button ("b1");
  b2=new Button ("b2");
  b3=new Button ("b3");
  t1=new TextField(" ");
  t2=new TextField(" ");
  t3=new TextField(" ");
     
  b1.setBounds(10,20,50,20);
  add(b1);
  b2.setBounds(10,45,50,20);
  add(b2);
  b3.setBounds(10,70,50,20);
  add(b3);
      t1.setBounds(70,20,90,20);
          add(t1);
       t2.setBounds(70,45,90,20);
           add(t2);
       t3.setBounds(70,70,90,20);
            add(t3);

  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  }
 public void actionPerformed(ActionEvent ae)
 {
   String s=(String)ae.getActionCommand();

   if (s.equals("b1") )
   {
      t1.setText("This is b1");
   }
   if (s.equals("b2") )
   {
      t2.setText("This is b2");
   }
   if (s.equals("b3") )
   {
     t3.setText("This is b3");
   }
   repaint();
 }

public void paint(Graphics g)
 {
  g.drawString(msg,60,105);
 }

 
}


No comments:

Post a Comment