// Fig. 11.15: ButtonFrame.java // Creating JButtons. import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class ButtonFrame extends JFrame { private JButton plainJButton; // button with just text private JButton fancyJButton; // button with icons // ButtonFrame adds JButtons to JFrame public ButtonFrame() { super( "Testing Buttons" ); setLayout( new FlowLayout() ); // set frame layout plainJButton = new JButton( "Plain Button" ); // button with text add( plainJButton ); // add plainJButton to JFrame Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) ); Icon bug2 = new ImageIcon( getClass().getResource( "bug2.gif" ) ); fancyJButton = new JButton( "Fancy Button", bug1 ); // set image fancyJButton.setRolloverIcon( bug2 ); // set rollover image add( fancyJButton ); // add fancyJButton to JFrame // create new ButtonHandler for button event handling ButtonHandler handler = new ButtonHandler(); fancyJButton.addActionListener( handler ); plainJButton.addActionListener( handler ); } // end ButtonFrame constructor // inner class for button event handling private class ButtonHandler implements ActionListener { // handle button event public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( ButtonFrame.this, String.format( "You pressed: %s", event.getActionCommand() ) ); } // end method actionPerformed } // end private inner class ButtonHandler } // end class ButtonFrame /************************************************************************** * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/