The necessary steps have been taken, below.
You could have picked another name besides WindowQuitter
for the
listener class.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoButtons extends JFrame implements ActionListener { JButton redButton ; JButton grnButton ; // constructor for TwoButtons public TwoButtons() { redButton = new JButton("Red"); grnButton = new JButton("Green"); getContentPane().setLayout( new FlowLayout() ); getContentPane().add( redButton ); getContentPane().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 ); } }
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
.