// Purpose. JButton and "anonymous inner class" demo import javax.swing.*; import java.awt.event.*; public class JButtonDemo { public static JPanel createButtons( String[] choices ) { JPanel buttons = new JPanel(); ActionListener bl = new ActionListener() { public void actionPerformed( ActionEvent e ) { System.out.println( e.getActionCommand() ); } }; JButton b; for (int i=0; i < choices.length; i++) { b = new JButton( choices[i] ); b.addActionListener( bl ); buttons.add( b ); } return buttons; } public static void main( String[] args ) { String[] choices = { "Alpha","Bravo","Charlie","Delta","Echo","Foxtrot" }; JFrame frame = new JFrame( "JButton demo" ); // 1. class definition // class WL extends WindowAdapter { // public void windowClosing( WindowEvent e ) { // System.exit( 0 ); // } } // 2. object creation // WL wlObj = new WL(); // 3. object registration // frame.addWindowListener( wlObj ); // 1. class definition // class WL extends WindowAdapter { // public void windowClosing( WindowEvent e ) { // System.exit( 0 ); // } } // 2. object creation and object registration // frame.addWindowListener( new WL() ); // 1. class definition, object creation, and object registration frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); frame.getContentPane().add( createButtons( choices ) ); frame.setSize( 300, 240 ); frame.setVisible( true ); } }