Java Reference
In-Depth Information
Labels are objects of a particular class type. The Java class that you use to create labels
is JLabel . Therefore, to create labels, you instantiate objects of type JLabel . The class
JLabel is contained in the package javax.swing .
Just like a window, various attributes are associated with a label. For example, every label
has a title, width, and height. The class JLabel contains various methods to control the
display of labels. Table 6-3 describes some of the methods provided by the class
JLabel .
TABLE 6-3 Some Methods Provided by the class JLabel
Method / Description/ Example
public JLabel(String str)
//Constructor to create a label with left-aligned text specified
//by str.
//Example: JLabel lengthL;
//
lengthL = new JLabel("Enter the length:")
//
Creates the label lengthL with the title Enter the length:
public JLabel(String str, int align)
//Constructor to create a label with the text specified by str.
// The value of align can be any one of the following:
// SwingConstants.LEFT, SwingConstants.RIGHT,
// SwingConstants.CENTER
//Example:
//
JLabel lengthL;
//
lengthL = new JLabel("Enter the length:",
//
SwingConstants.RIGHT);
//
The label lengthL is right aligned.
public JLabel(String t, Icon icon, int align)
//Constructs a JLabel with both text and an icon.
//The icon is to the left of the text.
public JLabel(Icon icon)
//Constructs a JLabel with an icon.
In Table 6-3, SwingConstants.LEFT , SwingConstants.RIGHT , and
SwingConstants.CENTER are constants defined in the class SwingConstants .
They specify whether to set the string describing the label as left-justified, right-justified,
or centered.
Search WWH ::




Custom Search