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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.