13.21. The Label Class

Labels are simple textual displays. It is often more convenient to use a Label than to draw text with the drawString method of Graphics because labels are 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, you may use the label to describe some other object and want to be sure the label is aligned with it. So, you may want to use the label with a layout manager other than FlowLayout. Changing the label font 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 ...

Get Core Web Programming, Second Edition now with O’Reilly online learning.

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