// Simplified from Sun's ButtonDemo.java. // 5/28/04 import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; /* * ButtonDemo.java is a 1.4 application that requires the following files: * images/right.gif * images/middle.gif * images/left.gif */ public class ButtonDemo extends JPanel implements ActionListener { protected int greetingCount; protected JButton b1, b2; public ButtonDemo() { greetingCount = 0; b1 = new JButton( "Say Hi" ); b1.setActionCommand( "hi" ); b2 = new JButton( "Cancel" ); b2.setActionCommand( "cancel" ); b1.addActionListener(this); b2.addActionListener(this); b1.setToolTipText( "Click this button for a greeting." ); b2.setToolTipText( "Click this button to quit." ); //Add Components to this container, using the default FlowLayout. add( b1 ); add( b2 ); } public void actionPerformed( ActionEvent e ) { if( "hi".equals( e.getActionCommand() ) ) { greetingCount++; System.out.println( "Hi " + greetingCount ); } else { System.exit( 1 ); } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated( true ); //Create and set up the window. JFrame frame = new JFrame( "ButtonDemo" ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //Create and set up the content pane. ButtonDemo newContentPane = new ButtonDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main( String[] args ) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }