| JLabel |
|---|
JLabel is used exactly like Label:
as a way to display text. However, since a JLabel can have
an image instead of or in addition to the text, it is also frequently used
as a way to put an image in a display.
The first is the ability to display images, usually by supplying an ImageIcon
eiher to the constructor or via a call to setIcon. The use of icons in
JLabel is just like the use in JButton, so see the
Swing Tutorial JButton section for more details
and code examples. For a quick preview, however, see the third
JLabel in the example below.
The second new feature is the ability to place borders around the labels. The use of borders is covered in the Swing tutorial section on JPanel. For a quick preview, however, see the following examples which use titled borders.
|
|---|
| JLabels with HTML Text |
The third new feature, and the one I am focusing on here, is the ability to
use HTML to format the label. The idea is that, if the string
for the label begins with "<html>", then the string is interpreted
as HTML rather than taken literally. This lets you make multi-line labels,
labels with mixed colors and fonts, and various other fancy effects.
This capability also applies to JButton. Although nice,
this capabillity also has several significant limitations:
setIcon or supply an ImageIcon to
the JLabel constructor (as with the third label in the
example shown above); the HTML cannot have an IMG tag.
JLabel label = new JLabel("<html>Bold Text");
label.setFont(new Font("Serif", Font.PLAIN, 36));
...
Sun says that this will probably not be fixed until after the
first release of JDK 1.3.
JLabel or
JButton works like <BR> does in "real" HTML, starting a new
line but not leaving a blank line in between.
import java.awt.*;
import javax.swing.*;
public class JLabels extends JFrame {
public static void main(String[] args) {
new JLabels();
}
public JLabels() {
super("Using HTML in JLabels");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
Font font = new Font("Serif", Font.PLAIN, 30);
content.setFont(font);
String labelText =
"<html><FONT COLOR=RED>Red</FONT> and " +
"<FONT COLOR=BLUE>Blue</FONT> Text</html>";
JLabel coloredLabel =
new JLabel(labelText, JLabel.CENTER);
coloredLabel.setBorder
(BorderFactory.createTitledBorder("Mixed Colors"));
content.add(coloredLabel, BorderLayout.NORTH);
labelText =
"<html><B>Bold</B> and <I>Italic</I> Text</html>";
JLabel boldLabel =
new JLabel(labelText, JLabel.CENTER);
boldLabel.setBorder
(BorderFactory.createTitledBorder("Mixed Fonts"));
content.add(boldLabel, BorderLayout.CENTER);
labelText =
"<html>The Applied Physics Laboratory is a division " +
"of the Johns Hopkins University." +
"<P>" +
"Major JHU divisions include:" +
"<UL>" +
" <LI>The Applied Physics Laboratory" +
" <LI>The Krieger School of Arts and Sciences" +
" <LI>The Whiting School of Engineering" +
" <LI>The School of Medicine" +
" <LI>The School of Public Health" +
" <LI>The School of Nursing" +
" <LI>The Peabody Institute" +
" <LI>The Nitze School of Advanced International Studies" +
"</UL>";
JLabel fancyLabel =
new JLabel(labelText,
new ImageIcon("images/JHUAPL.gif"),
JLabel.CENTER);
fancyLabel.setBorder
(BorderFactory.createTitledBorder("Multi-line HTML"));
content.add(fancyLabel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
Note: also requires
WindowUtilities.java
and ExitListener.java,
shown earlier, plus
JHUAPL.gif.