The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
With the 使用JLabel
class, you can display unselectable text and images. JLabel
类,可以显示不可选择的文本和图像。If you need to create a component that displays a string, an image, or both, you can do so by using or extending 如果需要创建一个显示字符串、图像或两者的组件,可以使用或扩展JLabel
. JLabel
。If the component is interactive and has a certain state, use a button instead of a label.如果组件是交互式的并且具有特定状态,则使用按钮而不是标签。
By specifying HTML code in a label's text, you can give the label various characteristics such as multiple lines, multiple fonts or multiple colors. 通过在标签的文本中指定HTML代码,可以为标签提供各种特性,如多行、多字体或多种颜色。If the label uses just a single color or font, you can avoid the overhead of HTML processing by using the 如果标签仅使用单一颜色或字体,则可以使用setForeground
or setFont
method instead. setForeground
或setFont
方法来避免HTML处理的开销。See Using HTML in Swing Components for details.有关详细信息,请参阅在Swing组件中使用HTML。
Note that labels are not opaque by default. 请注意,默认情况下标签不是不透明的。If you need to paint the label's background, it is recommended that you turn its opacity property to "true". 如果需要绘制标签的背景,建议将其不透明度属性设置为“true
”。The following code snippet shows how to do this.下面的代码片段显示了如何做到这一点。
label.setOpaque(true);
The following picture introduces an application that displays three labels. 下图介绍了一个显示三个标签的应用程序。The window is divided into three rows of equal height; the label in each row is as wide as possible.窗口分为三行,高度相等;每行中的标签尽可能宽。
Below is the code from 下面是LabelDemo.java
that creates the labels in the previous example.LabelDemo.java
中的代码,用于创建前面示例中的标签。
ImageIcon icon = createImageIcon("images/middle.gif"); . . . label1 = new JLabel("Image and Text", icon, JLabel.CENTER); //Set the position of the text, relative to the icon: label1.setVerticalTextPosition(JLabel.BOTTOM); label1.setHorizontalTextPosition(JLabel.CENTER); label2 = new JLabel("Text-Only Label"); label3 = new JLabel(icon);
The code for the createImageIcon
method is similar to that used throughout this tutorial. createImageIcon
方法的代码与本教程中使用的代码类似。You can find it in How to Use Icons.您可以在如何使用图标中找到它。
Often, a label describes another component. 通常,标签描述另一个组件。When this occurs, you can improve your program's accessibility by using the 发生这种情况时,可以通过使用setLabelFor
method to identify the component that the label describes. setLabelFor
方法来标识标签描述的组件,从而提高程序的可访问性。For example:例如:
amountLabel.setLabelFor(amountField);
The preceding code, taken from the 前面的代码取自如何使用格式化文本字段中讨论的FormattedTextFieldDemo
example discussed in How to Use Formatted Text Fields, lets assistive technologies know that the label (amountLabel
) provides information about the formatted text field (amountField
). FormattedTextFieldDemo
示例,它让辅助技术知道标签(amountLabel
)提供了有关格式化文本字段(amountField
)的信息。For more information about assistive technologies, see How to Support Assistive Technologies.有关辅助技术的更多信息,请参阅如何支持辅助技术。
The following tables list the commonly used 下表列出了常用的JLabel
constructors and methods. JLabel
构造函数和方法。Other methods you are likely to call are defined by the 您可能调用的其他方法由Component
and JComponent
classes. Component
和JComponent
类定义。They include 它们包括setFont
, setForeground
, setBorder
, setOpaque
, and setBackground
. setFont
、setForeground
、setBorder
、setOpaque
和setBackground
。See The JComponent Class for details. 有关详细信息,请参阅JComponent类。The API for using labels falls into three categories:使用标签的API分为三类:
In the following API, do not confuse label alignment with X and Y alignment. 在以下API中,不要将标签对齐与X和Y对齐混淆。X and Y alignment are used by layout managers and can affect the way any component not just a label is sized or positioned. 布局管理器使用X和Y对齐,并且可以影响任何组件的方式;不仅仅是标签确定尺寸或定位。Label alignment, on the other hand, has no effect on a label's size or position. 另一方面,标签对齐对标签的大小或位置没有影响。Label alignment simply determines where, inside the label's painting area, the label's contents are positioned. 标签对齐仅确定标签内容在标签的绘制区域内的位置。Typically, the label's painting area is exactly the size needed to paint on the label and thus label alignment is irrelevant. 通常,标签的绘制区域正好是在标签上绘制所需的大小,因此标签对齐不相关。For more information about X and Y alignment, see How to Use BoxLayout.有关X和Y对齐的更多信息,请参阅如何使用BoxLayout。
JLabel(Icon) JLabel(Icon, int) JLabel(String) JLabel(String, Icon, int) JLabel(String, int) JLabel()
|
JLabel instance, initializing it to have the specified text/image/alignment. JLabel 实例,并将其初始化为具有指定的文本/图像/对齐方式。int argument specifies the horizontal alignment of the label's contents within its drawing area. int 参数指定标签内容在其绘图区域内的水平对齐方式。SwingConstants interface (which JLabel implements): LEFT , CENTER , RIGHT , LEADING , or TRAILING . SwingConstants 接口(JLabel 实现)中定义的以下常量之一:LEFT 、CENTER 、RIGHT 、LEADING 或TRAILING 。LEADING and TRAILING , rather than LEFT and RIGHT .LEADING 和TRAILING ,而不是LEFT 和RIGHT 。 |
void setText(String) String getText() |
|
void setIcon(Icon) Icon getIcon() |
|
void setDisplayedMnemonic(char) char getDisplayedMnemonic() |
setLabelFor ), then when the user activates the mnemonic, the keyboard focus is transferred to the component specified by the labelFor property.setLabelFor ),则当用户激活助记符时,键盘焦点将转移到labelFor 属性指定的组件。 |
void setDisplayedMnemonicIndex(int) int getDisplayedMnemonicIndex() |
setDisplayedMnemonicIndex(5) decorates the character that is at position 5 (that is, the 6th character in the text). setDisplayedNemonicIndex(5) 修饰位于位置5的字符(即文本中的第6个字符)。 |
void setDisabledIcon(Icon) Icon getDisabledIcon() |
void setHorizontalAlignment(int) void setVerticalAlignment(int) int getHorizontalAlignment() int getVerticalAlignment() |
SwingConstants interface defines five possible values for horizontal alignment: LEFT , CENTER (the default for image-only labels), RIGHT , LEADING (the default for text-only labels), TRAILING . LEFT 、CENTER (仅图像标签的默认值)、RIGHT 、LEADING (仅文本标签的默认)、TRAILING 。TOP , CENTER (the default), and BOTTOM .SwingConstants 对于垂直对齐:TOP 、CENTER (默认值)和BOTTOM 。 |
void setHorizontalTextPosition(int) void setVerticalTextPosition(int) int getHorizontalTextPosition() int getVerticalTextPosition() |
SwingConstants interface defines five possible values for horizontal position: LEADING , LEFT , CENTER , RIGHT , and TRAILING (the default). SwingConstants 接口为水平位置定义了五个可能的值:LEADING 、LEFT 、CENTER 、RIGHT 和TRAILING (默认值)。TOP , CENTER (the default), and BOTTOM .TOP 、CENTER (默认值)和BOTTOM 。 |
void setIconTextGap(int) int getIconTextGap() |
void setLabelFor(Component) Component getLabelFor() |
The following table lists some of the many examples that use labels.下表列出了许多使用标签的示例。
LabelDemo |
||
HtmlDemo |
||
BoxAlignmentDemo |
||
DialogDemo |
||
SplitPaneDemo |
||
SliderDemo2 |
JLabel to provide labels for a slider.JLabel 为滑块提供标签。 | |
TableDialogEditDemo |
ColorRenderer , to display colors in table cells.ColorRenderer ,以在表单元格中显示颜色。 | |
FormattedTextFieldDemo |
||
TextComponentDemo |
TextComponentDemo CaretListenerLabel ) that extends JLabel to provide a label that listens for events, updating itself based on the events.CaretListenerLabel ),该类扩展JLabel 以提供一个监听事件的标签,并根据事件更新自身。 | |
ColorChooserDemo |
See the Using JavaFX UI Controls: Label tutorial to learn about JavaFX labeled controls.请参阅使用JavaFXUI控件:标签教程以了解JavaFX标签控件。