Labels are simple textual displays, without any associated actions, at least in Java 1.0. It is often more convenient to use them than to draw text with the drawString method of Graphics because labels get redisplayed automatically and can be moved around by layout managers. You can just create one with designated text and drop it in a window, as follows:
Label label = new Label("...");
add(label);
Frequently, however, I want the Label to describe some other object, and want to be sure the label is aligned with it. So I may use the label with a layout manager other than FlowLayout. Changing the label font and/or alignment is common as well. For instance, the following might be used to make a title on a panel:
Panel resultsPanel = new Panel();
resultsPanel.setLayout(new BorderLayout());
Label title = new Label("Results", Label.CENTER);
title.setFont(new Font("Helvetica", Font.BOLD, 18));
resultsPanel.add("North", title);
TextArea resultsArea = new TextArea();
resultsPanel.add("Center", resultsArea);
This creates a label displaying the specified string.
This creates a label with the specified alignment. The alignment is one of Label.LEFT (default), Label.RIGHT, and Label.CENTER. The alignment is not particularly important if the label is used in a window that is using FlowLayout or in the East or West regions of a BorderLayout. That is because the preferred width of a Label is only a little bit bigger than the text it contains, and there's not much aligning left to be done. However, alignment is quite important if you resize a Label by hand or use one in a GridLayout, GridBagLayout, or the North or South regions of a BorderLayout. In such a case, the width of the Label might be much larger than the text it contains, and Java needs to know where to place the text in that area.
This creates a blank label. You can specify the text later via setText.
Listing 13.30 creates four labels with various fonts and alignment options. Figure 13-28 shows the result.
Listing 13.30
|
|---|
import java.applet.Applet;
import java.awt.*;
public class Labels extends Applet {
public void init() {
setLayout(new GridLayout(4,1));
Label label1, label2, label3, label4;
label1 = new Label("Label 1");
label2 = new Label("Label 2", Label.LEFT);
label3 = new Label("Label 3", Label.RIGHT);
label4 = new Label("Label 4", Label.CENTER);
Font bigFont = new Font("Helvetica",
Font.BOLD, 25);
label2.setFont(bigFont);
label3.setFont(bigFont);
label4.setFont(bigFont);
add(label1);
add(label2);
add(label3);
add(label4);
}
}
|
This returns the current alignment, which will be one of Label.LEFT, Label.RIGHT, or Label.CENTER.
This returns the text of the label. If none has been set, a zero-length (but non-null) string is returned.
This sets the alignment.
This method changes the label's text. If the label is already displayed, doing this does not automatically resize it in its Container. So the containing window should be invalidated and validated to force a new layout, as follows.
someLabel.setText("A Different Label");
someLabel.getParent().invalidate();
someLabel.getParent().validate();
As a Component subclass, Label inherits all the color, font, and resizing methods of Component (Section 11.2). However, as with several other GUI controls, support for colors is spotty on Windows 95/NT implementations. In particular, neither Netscape 2, Internet Explorer 3, nor Sun JDK 1.02 support background or foreground colors for labels on Windows. Label colors are supported in Netscape 3, Netscape 4 (i.e. Netscape Communicator), and JDK 1.1 on Windows, as well as in virtually all MacOS and Unix implementations.
In Java 1.0, no events are generated for labels; mouse and keyboard events are suppressed. In Java 1.1, the normal mouse and keyboard events are generated, but there are no selection or action events. Listing 13.31 shows a frame that inserts two "reversible" labels (Listing 13.32); labels with attached mouse listeners that switch the foreground and background colors when the mouse enters the label, switching back when the mouse leaves. Figures 13-29, 13-30, and 13-31 show the original result, the result after moving the mouse over the first ReversibleLabel, and the result after moving the mouse over the second ReversibleLabel, respectively.
Listing 13.31
|
|---|
import java.awt.*;
// Java 1.1 Only
public class ReverseLabels extends CloseableFrame {
public static void main(String[] args) {
new ReverseLabels();
}
public ReverseLabels() {
super("Reversible Labels");
setLayout(new FlowLayout());
setBackground(Color.lightGray);
setFont(new Font("Serif", Font.BOLD, 18));
ReversibleLabel label1 =
new ReversibleLabel("Black on White",
Color.white, Color.black);
add(label1);
ReversibleLabel label2 =
new ReversibleLabel("White on Black",
Color.black, Color.white);
add(label2);
pack();
setVisible(true);
}
}
|
Listing 13.32
|
|---|
import java.awt.*;
import java.awt.event.*;
/** A Label that reverses its background and
* foreground colors when the mouse is over it.
* Java 1.1 Only
*/
public class ReversibleLabel extends Label {
public ReversibleLabel(String text,
Color bgColor, Color fgColor) {
super(text);
MouseAdapter reverser =
new MouseAdapter() {
public void mouseEntered(MouseEvent event) {
reverseColors();
}
public void mouseExited(MouseEvent event) {
reverseColors(); // or mouseEntered(event);
}
};
addMouseListener(reverser);
setText(text);
setBackground(bgColor);
setForeground(fgColor);
}
protected void reverseColors() {
Color fg = getForeground();
Color bg = getBackground();
setForeground(bg);
setBackground(fg);
}
}
|
ReverseLabels: original
appearance.
ReverseLabels: after moving mouse over left
label.
ReverseLabels: after moving mouse over right
label.| Continue to Section 13.11 (Scrollbars and Sliders). | Return to Chapter 13 table of contents. |
|---|