Labels
Swing allows you to create labels that can contain text,
images, or both. We’ll begin this chapter with a look at the JLabel class.
The JLabel class allows
you to add basic, noninteractive labels to a user interface. Because of
its inherent simplicity, there is no model class for JLabel. Figure 4-1 shows a class diagram
for JLabel. We’ll get into the two
relationships to Icon a little
later.

Figure 4-1. JLabel class diagram
JLabel objects may consist of
both text and graphics (icons), but for simple text-only labels, the
interface with JLabel is very similar
to that of java.awt.Label. The code
to create and display a very simple text label looks like this:
// SimpleJLabelExample.java
//
import javax.swing.*;
public class SimpleJLabelExample {
public static void main(String[] args) {
JLabel label = new JLabel("A Very Simple Text Label");
JFrame frame = new JFrame( );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane( ).add(label); // Adds to CENTER
frame.pack( );
frame.setVisible(true);
}
}Running this simple program produces the display shown in Figure 4-2.
Figure 4-2. A simple JLabel
Properties
The JLabel class contains the
properties shown in Table
4-1. The icon and disabledIcon properties specify the icon to be displayed by default and when the label ...