// Fig. 13.10: ButtonTest.java // Creating JButtons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest2 extends JFrame { private JButton fancyButton; private JLabel plainButton; // set up GUI public ButtonTest2() { super( "Testing Buttons" ); // get content pane and set its layout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // create buttons Icon blueSquare = new ImageIcon( "blueSquare.jpg" ); plainButton = new JLabel( blueSquare ); container.add( plainButton ); Icon bug1 = new ImageIcon( "bug1.gif" ); Icon bug2 = new ImageIcon( "bug2.gif" ); fancyButton = new JButton( "Fancy Button", bug1 ); fancyButton.setRolloverIcon( bug2 ); container.add( fancyButton ); // create an instance of inner class ButtonHandler // to use for button event handling ButtonHandler handler1 = new ButtonHandler("Instance1"); ButtonHandler handler2 = new ButtonHandler("Instance2"); fancyButton.addActionListener( handler1 ); //plainButton.addActionListener( handler2 ); plainButton.addMouseListener (new ButtonHandler2 ("Value1")); setSize( 275, 100 ); setVisible( true ); } // end ButtonTest constructor public static void main( String args[] ) { ButtonTest2 application = new ButtonTest2(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // inner class for button event handling private class ButtonHandler implements ActionListener { private String data; public ButtonHandler (String str) { data = str; } // handle button event public void actionPerformed (ActionEvent event ) { JOptionPane.showMessageDialog( ButtonTest2.this, "You pressed: " + "; " + data); } } // end private inner class ButtonHandler // inner class for button event handling private class ButtonHandler2 extends MouseAdapter { private String data; public ButtonHandler2 (String str) { data = str; } public void mouseExited (MouseEvent e) {} // handle button event public void mouseClicked ( MouseEvent event ) { // JOptionPane.showMessageDialog( ButtonTest2.this, // "In ButtonHandler2: Click Count: " + event.getClickCount() + // "; Button NUmber: " + event.getButton()+ "; " + data); System.out.println ( "In ButtonHandler2: Click Count: " + event.getClickCount() + "; Button NUmber: " + event.getButton()+ "; " + data); } } // end private inner class ButtonHandler } // end class ButtonTest /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and * * Prentice Hall. 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. * *************************************************************************/