A JLabel object can display
either text, an image, or both.
You can specify where in the label's display area
the label's contents are aligned
by setting the vertical and horizontal alignment.
By default, labels are vertically centered
in their display area.
Text-only labels are leading edge aligned, by default;
image-only labels are horizontally centered, by default.
You can also specify the position of the text relative to the image. By default, text is on the trailing edge of the image, with the text and image vertically aligned.
A label's leading and trailing edge are determined from the value of its ComponentOrientation property. At present, the default ComponentOrientation setting maps the leading edge to left and the trailing edge to right.
Finally, you can use the setIconTextGap method
to specify how many pixels
should appear between the text and the image.
The default is 4 pixels.
See How to Use Labels in The Java Tutorial for further documentation.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see XMLEncoder .
| Nested Class Summary | |
|---|---|
| protected class |
|
| Field Summary | |
|---|---|
| protected Component |
No description provided. |
| Fields inherited from java.awtComponent |
|---|
| Fields inherited from javax.swingJComponent |
|---|
| Constructor Summary |
|---|
|
Creates a JLabel instance with the specified
text, image, and horizontal alignment.
|
|
Creates a JLabel instance with the specified
text and horizontal alignment.
|
|
Creates a JLabel instance with the specified text.
|
|
Creates a JLabel instance with the specified
image and horizontal alignment.
|
|
Creates a JLabel instance with the specified image.
|
|
Creates a JLabel instance with
no image and with an empty string for the title.
|
| Method Summary | |
|---|---|
| protected int |
Verify that key is a legal value for the horizontalAlignment properties. |
| protected int |
Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties. |
| AccessibleContext |
Get the AccessibleContext of this object |
| Icon |
Returns the icon used by the label when it's disabled. |
| int |
Return the keycode that indicates a mnemonic key. |
| int |
Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character. |
| int |
Returns the alignment of the label's contents along the X axis. |
| int |
Returns the horizontal position of the label's text, relative to its image. |
| Icon |
Returns the graphic image (glyph, icon) that the label displays. |
| int |
Returns the amount of space between the text and the icon displayed in this label. |
| Component |
Get the component this is labelling. |
| String |
Returns the text string that the label displays. |
| LabelUI |
Returns the L&F object that renders this component. |
| String |
Returns a string that specifies the name of the l&f class that renders this component. |
| int |
Returns the alignment of the label's contents along the Y axis. |
| int |
Returns the vertical position of the label's text, relative to its image. |
| boolean |
This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
|
| protected String |
Returns a string representation of this JLabel. |
| void |
Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)). |
| void |
Specify a keycode that indicates a mnemonic key. |
| void |
Specifies the displayedMnemonic as a char value. |
| void |
Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic. |
| void |
Sets the alignment of the label's contents along the X axis. |
| void |
Sets the horizontal position of the label's text, relative to its image. |
| void |
Defines the icon this component will display. |
| void |
If both the icon and text properties are set, this property defines the space between them. |
| void |
Set the component this is labelling. |
| void |
Defines the single line of text this component will display. |
| void |
Sets the L&F object that renders this component. |
| void |
Sets the alignment of the label's contents along the Y axis. |
| void |
Sets the vertical position of the label's text, relative to its image. |
| void |
Resets the UI property to a value from the current look and feel. |
| Methods inherited from java.awtMenuContainer |
|---|
| Methods inherited from java.langObject |
|---|
| Methods inherited from javax.swingTransferHandler.HasGetTransferHandler |
|---|
JLabel instance with the specified
text, image, and horizontal alignment.
The label is centered vertically in its display area.
The text is on the trailing edge of the image. JLabel instance with the specified
text and horizontal alignment.
The label is centered vertically in its display area. JLabel instance with the specified text.
The label is aligned against the leading edge of its display area,
and centered vertically. JLabel instance with the specified
image and horizontal alignment.
The label is centered vertically in its display area. JLabel instance with the specified image.
The label is centered vertically and horizontally
in its display area.
public JLabel
(
)
JLabel instance with
no image and with an empty string for the title.
The label is centered vertically
in its display area.
The label's contents, once set, will be displayed on the leading edge
of the label's display area. Some look and feels might not render the disabled Icon, in which case they will ignore this.
public
int
getDisplayedMnemonic
(
)
public
int
getDisplayedMnemonicIndex
(
)
public
int
getHorizontalAlignment
(
)
public
int
getHorizontalTextPosition
(
)
public
int
getIconTextGap
(
)
public
int
getVerticalAlignment
(
)
public
int
getVerticalTextPosition
(
)
img. null. The default value of this property is null.
public
void
setDisplayedMnemonic
(
int
key
)
public
void
setDisplayedMnemonic
(
char
aChar
)
The value of this is updated as the properties relating to the
mnemonic change (such as the mnemonic itself, the text...).
You should only ever have to call this if
you do not wish the default character to be underlined. For example, if
the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
to be decorated, as 'Save As', you would have to invoke
setDisplayedMnemonicIndex(5) after invoking
setDisplayedMnemonic(KeyEvent.VK_A).
public
void
setHorizontalAlignment
(
int
alignment
)
This is a JavaBeans bound property.
public
void
setHorizontalTextPosition
(
int
textPosition
)
The default value of this property is null.
This is a JavaBeans bound property.
public
void
setIconTextGap
(
int
iconTextGap
)
The default value of this property is 4 pixels.
This is a JavaBeans bound property.
The default value of this property is null.
This is a JavaBeans bound property.
public
void
setVerticalAlignment
(
int
alignment
)
The default value of this property is CENTER.
public
void
setVerticalTextPosition
(
int
textPosition
)
The default value of this property is CENTER.
This is a JavaBeans bound property.
public
void
updateUI
(
)