Action Event Listener
You could have picked another name besides WindowQuitter for the
listener class.
import java.awt.*;
import java.awt.event.*;
public class TwoButtons extends Frame implements ActionListener
{
Button redButton = new Button("Red");
Button grnButton = new Button("Green");
TwoButtons() // constructor for TwoButtons
{
setLayout( new FlowLayout() ); // choose the layout manager
add( redButton );
add( grnButton );
}
. . . . more code will go here . . . .
public static void main ( String[] args )
{
TwoButtons demo = new TwoButtons() ;
WindowQuitter wquit = new WindowQuitter(); // 2
demo.addWindowListener( wquit ); // 3
demo.setSize( 200, 150 );
demo.setVisible( true );
}
}
class WindowQuitter extends WindowAdapter // 1
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 ); // what to do for this event--exit the program
}
}
|
An action listener needs to be registered for the two buttons.
There are various ways that this could be done,
but let us use one listener that will listen to both
of the buttons.
The listener object will be the same object as the
container, the object of type TwoButtons.
|