9.8. (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels

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 identifying GUI components on the screen and keeping the user informed about the current state of the 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.

Figure 9.19. JLabel with text and with images.
 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; ...

Get Java™ How to Program, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.