Example of a button and an action listener
Consider an applet that looks the
following running:
When the button is clicked, a
message 'ouch' is displayed in a dialogue window:
//<APPLET
CODE = "Listener1.class" height = 100 width =
200></APPLET>
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Listener1 extends Applet
implements ActionListener
{
String
message = "";
public
Listener1()
{
super();
Button myButton = new Button ("Click me!");
add(myButton);
myButton.addActionListener(this);
}
public void
actionPerformed (ActionEvent e)
{
message = "ooch!!!";
repaint();
}
public void
paint(Graphics g)
{
g.drawString(message,75,50);
}
} // class
|