Following example is to demonstrate button click event using actionListener and ActionEvent in java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/* <Applet code=buttonclick width=300 height=300> </Applet> */
public class buttonclick extends Applet implements ActionListener
{ TextField text1;
Button button1;
Label l1;
public void init()
{ l1=new Label("Message: ");
add(l1);
text1=new TextField(15);
add(text1);
button1=new Button("Click");
add(button1);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e1)
{ String str="Hello, java is Great";
if(e1.getSource()==button1)
text1.setText(str);
}
}
To compile this program: Goto command prompt type "javac buttonclick.java"
To execute this program: Type "appletviewer buttonclick.java"
Comments
Post a Comment