JLabel Class
Package: javax.swing
The JLabel
class represents a label — a user interface component that simply displays text. Labels are used for a variety of purposes: display captions for other controls (such as text fields or combo boxes), informational messages, or results of a calculation or a database lookup.
Constructors
Constructor |
Description |
|
Creates a new label with no initial text |
|
Creates a new label with the specified text |
Methods
Method |
Description |
|
Returns the text displayed by the label |
|
Sets the text displayed by the label |
|
Sets the tooltip text that displays if the user hovers the mouse over the label for a few moments |
|
Shows or hides the label |
When you create a label, you can pass the text you want it to display to the constructor, like this:
JLabel label1 = new JLabel(“Hello, World!”);
Or you can create the label first and then set its text later, as follows:
JLabel label1 = new JLabel();
label1.setText(“Hello, World!”);
To display a label, you must add it to a panel, which in turn must be added to a frame. Here’s an example of a constructor for a frame class that creates a panel, creates a label, adds the label to the panel, and then adds the panel to the frame:
// HelloFrame constructor
public HelloFrame()
{
this.setSize(300,150);
this.setDefaultCloseOperation( ...
Get Java For Dummies Quick Reference 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.