654 Graphical User Interface Components: Part (Web page design) 1 Chapter
654 Graphical User Interface Components: Part 1 Chapter 12 Class JLabelprovides many methods to configure a label after it has been instantiated. Line 37 creates a JLabeland invokes the no-argument (default constructor). Such a label has no text or Icon. Line 38 uses JLabel method setText to set the text displayed on the label. A corresponding method getTextretrieves the current text displayed on a label. Line 39 uses JLabelmethod setIconto set the Icondisplayed on the label. A corresponding method getIconretrieves the current Icondisplayed on a label. Lines 40 41 use JLabelmethods setHorizontalTextPositionand setVertical- TextPositionto specify the text position in the label. In this case, the text will be centered horizontally and will appear at the bottom of the label. Thus, the Icon will appear above the text. Line 42 sets the tool tip text for the label3. Line 43 adds label3to the content pane. 12.4 Event-Handling Model In the preceding section, we did not discuss event handling because there are no specific events for JLabelobjects. GUIs are event driven (i.e., they generate events when the user of the program interacts with the GUI). Some common interactions are moving the mouse, clicking the mouse, clicking a button, typing in a text field, selecting an item from a menu, closing a window, etc. When a user interaction occurs, an event is sent to the program. GUI event information is stored in an object of a class that extends AWTEvent. Figure 12.5 illustrates a hierarchy containing many of the event classes we use from package java.awt.event. Many of these event classes are discussed throughout this chapter and Chapter 13. The event types from package java.awt.eventare used with both AWT and Swing components. Additional event types have also been added that are specific to several types of Swing components. New Swing-component event types are defined in package javax.swing.event. Class name Key java.lang.Object java.awt.AWTEvent ActionEvent ItemEvent AdjustmentEvent ComponentEvent java.util.EventObject ContainerEvent PaintEvent FocusEvent WindowEvent InputEvent KeyEvent MouseEventInterface name Fig. 12.5Some event classes of package java.awt.event. 12. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01