Source of ButtonFrame.java


  1: // Fig. 11.15: ButtonFrame.java
  2: // Creating JButtons.
  3: import java.awt.FlowLayout;
  4: import java.awt.event.ActionListener;
  5: import java.awt.event.ActionEvent;
  6: import javax.swing.JFrame;
  7: import javax.swing.JButton;
  8: import javax.swing.Icon;
  9: import javax.swing.ImageIcon;
 10: import javax.swing.JOptionPane;
 11: 
 12: public class ButtonFrame extends JFrame 
 13: {
 14:    private JButton plainJButton; // button with just text
 15:    private JButton fancyJButton; // button with icons
 16: 
 17:    // ButtonFrame adds JButtons to JFrame
 18:    public ButtonFrame()
 19:    {
 20:       super( "Testing Buttons" );
 21:       setLayout( new FlowLayout() ); // set frame layout
 22: 
 23:       plainJButton = new JButton( "Plain Button" ); // button with text
 24:       add( plainJButton ); // add plainJButton to JFrame
 25: 
 26:       Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) );
 27:       Icon bug2 = new ImageIcon( getClass().getResource( "bug2.gif" ) );
 28:       fancyJButton = new JButton( "Fancy Button", bug1 ); // set image
 29:       fancyJButton.setRolloverIcon( bug2 ); // set rollover image
 30:       add( fancyJButton ); // add fancyJButton to JFrame
 31: 
 32:       // create new ButtonHandler for button event handling 
 33:       ButtonHandler handler = new ButtonHandler();
 34:       fancyJButton.addActionListener( handler );
 35:       plainJButton.addActionListener( handler );
 36:    } // end ButtonFrame constructor
 37: 
 38:    // inner class for button event handling
 39:    private class ButtonHandler implements ActionListener 
 40:    {
 41:       // handle button event
 42:       public void actionPerformed( ActionEvent event )
 43:       {
 44:          JOptionPane.showMessageDialog( ButtonFrame.this, String.format(
 45:             "You pressed: %s", event.getActionCommand() ) );
 46:       } // end method actionPerformed
 47:    } // end private inner class ButtonHandler
 48: } // end class ButtonFrame
 49: 
 50: /**************************************************************************
 51:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 52:  * Pearson Education, Inc. All Rights Reserved.                           *
 53:  *                                                                        *
 54:  * DISCLAIMER: The authors and publisher of this book have used their     *
 55:  * best efforts in preparing the book. These efforts include the          *
 56:  * development, research, and testing of the theories and programs        *
 57:  * to determine their effectiveness. The authors and publisher make       *
 58:  * no warranty of any kind, expressed or implied, with regard to these    *
 59:  * programs or to the documentation contained in these books. The authors *
 60:  * and publisher shall not be liable in any event for incidental or       *
 61:  * consequential damages in connection with, or arising out of, the       *
 62:  * furnishing, performance, or use of these programs.                     *
 63:  *************************************************************************/