Chapter 12 Graphical User Interface Components: Part 1 (Free web host)
Chapter 12 Graphical User Interface Components: Part 1 651 Good Programming Practice 12.3 Study the methods of class JComponentin the Java 2 SDK on-line documentation to learn the capabilities common to every container for GUI components. Swing components that subclass JComponenthave many features, including: 1. A pluggable look and feel that can be used to customize the look and feel when the program executes on different platforms. 2. Shortcut keys (called mnemonics) for direct access to GUI components through the keyboard. 3. Common event-handling capabilities for cases where several GUI components initiate the same actions in a program. 4. Brief descriptions of a GUI component s purpose (called tool tips) that are displayed when the mouse cursor is positioned over the component for a short time. 5. Support for assistive technologies such as braille screen readers for blind people. 6. Support for user interface localization customizing the user interface for display in different languages and cultural conventions. These are just some of the many features of the Swing components. We discuss several of these features here and in Chapter 13. 12.3 JLabel Labels provide text instructions or information on a GUI. Labels are defined with class JLabel a subclass of JComponent. A label displays a single line of read-only text, an image or both text and an image. Programs rarely change a label s contents after creating it. The application of Figure 12.4 demonstrates several JLabelfeatures. 1 // Fig. 12.4: LabelTest.java 2 // Demonstrating the JLabel class. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class LabelTest extends JFrame { 12 private JLabel label1, label2, label3; 13 14 // set up GUI 15 public LabelTest() 16 { 17 super( “Testing JLabel” ); 18 Fig. 12.4 Demonstrating class JLabel(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01