Programs often use labels when they need to display information or instructions to the user in a graphical user interface. Labels are a convenient way of keeping the user informed about the current state of a program. In Java, an object of class JLabel (from package javax.swing) can display a single line of text, an image or both. The example in Fig. 9.19 demonstrates several JLabel features.
Lines 36 import the classes we need to display JLabels. BorderLayout from package java.awt contains constants that specify where we can place GUI components in the JFrame. Class ImageIcon represents an image that can be displayed on a JLabel, and class JFrame represents the window that will contain all the labels.
Line 13 creates a JLabel that displays its constructor argumentthe string "North". Line 16 declares local variable labelIcon and assigns it a new ImageIcon. The constructor for ImageIcon receives a String that specifies the path to the image. Since we only specify a file name, Java assumes that it is in the same directory as class LabelDemo. ImageIcon can load images in GIF, JPEG and PNG image formats. Line 19 declares and initializes local variable centerLabel with a JLabel that displays the labelIcon. Line 22 declares and initializes local variable southLabel with a JLabel similar to the one in line 19. However, line 25 calls method setText to change the text the label displays. Method setText can be called on any JLabel to change its text. This JLabel displays both the icon and the text.
Line 28 creates the JFrame that displays the JLabels, and line 30 indicates that the program should terminate when the JFrame is closed. We attach the labels to the JFrame in lines 3436 by calling an overloaded version of method add that takes two parameters. The first parameter is the component we want to attach, and the second is the region in which it should be placed. Each JFrame has an associated layout that helps the JFrame position the GUI components that are attached to it. The default layout for a JFrame is known as a BorderLayout and has five regionsNORTH (top), SOUTH (bottom), EAST (right side), WEST (left side) and CENTER. Each of these is declared as a constant in class BorderLayout. When calling method add with one argument, the JFrame places the component in the CENTER automatically. If a position already contains a component, then the new component takes its place. Lines 38 and 39 set the size of the JFrame and make it visible on screen.
Figure 9.19. JLabel with text and with images.
(This item is displayed on pages 453 - 454 in the print version)
1 // Fig 9.19: LabelDemo.java 2 // Demonstrates the use of labels. 3 import java.awt.BorderLayout; 4 import javax.swing.ImageIcon; 5 import javax.swing.JLabel; 6 import javax.swing.JFrame; 7 8 public class LabelDemo 9 { 10 public static void main( String args[] ) 11 { 12 // Create a label with plain text 13 JLabel northLabel = new JLabel( "North" ); 14 15 // create an icon from an image so we can put it on a JLabel 16 ImageIcon labelIcon = new ImageIcon( "GUItip.gif" ); 17 18 // create a label with an Icon instead of text 19 JLabel centerLabel = new JLabel( labelIcon ); 20 21 // create another label with an Icon 22 JLabel southLabel = new JLabel( labelIcon ); 23 24 // set the label to display text (as well as an icon) 25 southLabel.setText( "South" ); 26 27 // create a frame to hold the labels 28 JFrame application = new JFrame(); 29 30 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 31 32 // add the labels to the frame; the second argument specifies 33 // where on the frame to add the label 34 application.add( northLabel, BorderLayout.NORTH ); 35 application.add( centerLabel, BorderLayout.CENTER ); 36 application.add( southLabel, BorderLayout.SOUTH ); 37 38 application.setSize( 300, 300 ); // set the size of the frame 39 application.setVisible( true ); // show the frame 40 } // end main 41 } // end class LabelDemo |
GUI and Graphics Case Study Exercise
9.1 |
Modify Exercise 8.1 to include a JLabel as a status bar that displays counts representing the number of each shape displayed. Class DrawPanel should declare a method that returns a String containing the status text. In main, first create the DrawPanel, then create the JLabel with the status text as an argument to the JLabel's constructor. Attach the JLabel to the SOUTH region of the JFrame, as shown in Fig. 9.20. |
Figure 9.20. JLabel displaying shape statistics.
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover