664 Graphical User Interface Components: Part 1 Chapter (Apache web server)

664 Graphical User Interface Components: Part 1 Chapter 12 Fig. 12.10 Fig. 12.10 Demonstrating command buttons and action events (part 3 of 3). Line 12 declares two references to instances of class JButton plainButtonand fancyButton that are instantiated in the constructor. Line 24 creates plainButton with the button label “Plain Button”. Line 25 adds the button to the content pane. A JButtoncan display Icons. To provide the user with an extra level of visual inter- activity with the GUI, a JButton can also have a rollover Icon an Icon that is displayed when the mouse is positioned over the button. The icon on the button changes as the mouse moves in and out of the button s area on the screen. Lines 27 28 create two Image- Iconobjects that represent the default Iconand rollover Iconfor the JButtoncreated at line 29. Both statements assume the image files are stored in the same directory as the program (this is commonly the case for applications that use images). Line 29 creates fancyButton with default text “FancyButton”and the Icon bug1. By default, the text is displayed to the right of the icon. Line 30 uses method set- RolloverIcon (inherited from class AbstractButton into class JButton) to specify the image displayed on the button when the user positions the mouse over the button. Line 31 adds the button to the content pane. Look-and-Feel Observation 12.5 Using rollover icons for JButtons provides the user with visual feedback indicating that if they click the mouse, the button s action will occur. JButtons (like JTextFields) generate ActionEvents. As mentioned previously, an ActionEventcan be processed by any ActionListenerobject. Lines 35 37 register an ActionListener object for each JButton. Inner class ButtonHandler (lines 53 62) defines actionPerformed to display a message dialog box con Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply