Module java.desktop
Package javax.swing

Class JLabel

所有已实现的接口:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants
直接已知的子类:
BasicComboBoxRenderer, DefaultListCellRenderer, DefaultTableCellRenderer, DefaultTreeCellRenderer

@JavaBean(defaultProperty="UI", description="A component that displays a short string and an icon.") public class JLabel extends JComponent implements SwingConstants, Accessible
用于显示短文本字符串或图像的显示区域,或两者兼有。标签不会对输入事件做出反应。因此,它无法获得键盘焦点。但是,标签可以显示键盘替代项,以方便附近的组件显示具有键盘替代项但无法显示它的情况。

JLabel对象可以显示文本、图像或两者。您可以通过设置垂直和水平对齐方式来指定标签内容在标签显示区域中的位置。默认情况下,标签在其显示区域中垂直居中。仅文本标签默认为前导边缘对齐;仅图像标签默认为水平居中。

您还可以指定文本相对于图像的位置。默认情况下,文本位于图像的尾部,文本和图像垂直对齐。

标签的前导和尾部边缘是根据其ComponentOrientation属性的值确定的。目前,默认的ComponentOrientation设置将前导边缘映射到左侧,将尾部边缘映射到右侧。

最后,您可以使用setIconTextGap方法指定文本和图像之间应显示多少像素。默认值为4像素。

有关更多文档,请参阅The Java Tutorial中的如何使用标签

警告: Swing不是线程安全的。有关更多信息,请参见Swing的线程策略

警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同版本Swing的应用程序之间的RMI。从1.4开始,已将所有JavaBeans的长期存储支持添加到java.beans包中。请参见XMLEncoder

自 JDK 1.2 起:
1.2
  • Field Details

    • labelFor

      protected Component labelFor
      此标签所用的组件;如果标签不是组件的标签,则为null
  • Constructor Details

    • JLabel

      public JLabel(String text, Icon icon, int horizontalAlignment)
      创建一个带有指定文本、图像和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。文本位于图像的尾部。
      参数:
      text - 要由标签显示的文本。
      icon - 要由标签显示的图像。
      horizontalAlignment - SwingConstants中定义的以下常量之一: LEFTCENTERRIGHTLEADINGTRAILING
    • JLabel

      public JLabel(String text, int horizontalAlignment)
      创建一个带有指定文本和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。
      参数:
      text - 要由标签显示的文本。
      horizontalAlignment - SwingConstants中定义的以下常量之一: LEFTCENTERRIGHTLEADINGTRAILING
    • JLabel

      public JLabel(String text)
      创建一个带有指定文本的JLabel实例。标签相对其显示区域的前导边缘对齐,并在垂直方向上居中。
      参数:
      text - 要由标签显示的文本。
    • JLabel

      public JLabel(Icon image, int horizontalAlignment)
      创建一个带有指定图像和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。
      参数:
      image - 要由标签显示的图像。
      horizontalAlignment - SwingConstants中定义的以下常量之一: LEFTCENTERRIGHTLEADINGTRAILING
    • JLabel

      public JLabel(Icon image)
      创建一个带有指定图像的JLabel实例。标签在其显示区域中垂直和水平居中。
      参数:
      image - 要由标签显示的图像。
    • JLabel

      public JLabel()
      创建一个没有图像且标题为空字符串的JLabel实例。标签在其显示区域中垂直居中。一旦设置了标签的内容,它将显示在标签显示区域的前导边缘上。
  • Method Details

    • getUI

      public LabelUI getUI()
      返回呈现此组件的L&F对象。
      覆盖:
      getUI 在类 JComponent
      返回:
      LabelUI对象
    • setUI

      @BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(LabelUI ui)
      设置呈现此组件的L&F对象。
      参数:
      ui - LabelUI L&F对象
      参见:
    • updateUI

      public void updateUI()
      将UI属性重置为当前外观的值。
      覆盖:
      updateUI 在类 JComponent
      参见:
    • getUIClassID

      @BeanProperty(bound=false) public String getUIClassID()
      返回指定呈现此组件的l&f类的名称的字符串。
      覆盖:
      getUIClassID 在类 JComponent
      返回:
      字符串 "LabelUI"
      参见:
    • getText

      public String getText()
      返回标签显示的文本字符串。
      返回:
      一个字符串
      参见:
    • setText

      @BeanProperty(preferred=true, visualUpdate=true, description="Defines the single line of text this component will display.") public void setText(String text)
      定义组件将显示的单行文本。如果文本的值为null或空字符串,则不显示任何内容。

      此属性的默认值为null。

      这是一个JavaBeans绑定属性。

      参数:
      text - 组件将显示的单行文本
      参见:
    • getIcon

      public Icon getIcon()
      返回标签显示的图形图像(标志、图标)。
      返回:
      一个图标
      参见:
    • setIcon

      @BeanProperty(preferred=true, visualUpdate=true, description="The icon this component will display.") public void setIcon(Icon icon)
      定义组件将显示的图标。如果图标的值为null,则不显示任何内容。

      此属性的默认值为null。

      这是一个JavaBeans绑定属性。

      参数:
      icon - 组件将显示的默认图标
      参见:
    • getDisabledIcon

      public Icon getDisabledIcon()
      返回当JLabel被禁用时使用的图标。如果未设置禁用图标,则将调用外观和感觉来构造适当的禁用图标。

      某些外观和感觉可能不会呈现禁用图标,如果是这样,它们将忽略此设置。

      返回:
      disabledIcon 属性
      参见:
    • setDisabledIcon

      @BeanProperty(visualUpdate=true, description="The icon to display if the label is disabled.") public void setDisabledIcon(Icon disabledIcon)
      设置要显示的图标,如果此JLabel被"禁用"(JLabel.setEnabled(false))。

      此属性的默认值为null。

      参数:
      disabledIcon - 组件禁用时要显示的图标
      参见:
    • setDisplayedMnemonic

      @BeanProperty(visualUpdate=true, description="The mnemonic keycode.") public void setDisplayedMnemonic(int key)
      指定指示助记键的键代码。当标签是较大组件的一部分时,将使用此属性。如果标签的labelFor属性不为null,则在激活助记键时,标签将调用labelFor属性指定的组件的requestFocus方法。
      参数:
      key - 指示助记键的键代码
      参见:
    • setDisplayedMnemonic

      public void setDisplayedMnemonic(char aChar)
      将displayedMnemonic指定为char值。
      参数:
      aChar - 指定要显示的助记键的char值
      参见:
    • getDisplayedMnemonic

      public int getDisplayedMnemonic()
      返回指示助记键的键代码。当标签是较大组件的一部分时,将使用此属性。如果标签的labelFor属性不为null,则在激活助记键时,标签将调用labelFor属性指定的组件的requestFocus方法。
      返回:
      表示助记键的int值
      参见:
    • setDisplayedMnemonicIndex

      @BeanProperty(visualUpdate=true, description="the index into the String to draw the keyboard character mnemonic at") public void setDisplayedMnemonicIndex(int index) throws IllegalArgumentException
      向外观和感觉提供提示,指示文本中的哪个字符应该被装饰以表示助记键。并非所有外观和感觉都支持此功能。值为-1表示没有助记键,助记键字符不包含在字符串中,或开发人员不希望显示助记键。

      此值会随着与助记键相关的属性的更改(如助记键本身、文本等)而更新。只有在不希望默认字符被下划线时才需要调用此方法。例如,如果文本为'Save As',助记键为'a',您希望'A'被装饰为'Save As',则需要在调用setDisplayedMnemonic(KeyEvent.VK_A)后调用setDisplayedMnemonicIndex(5)

      参数:
      index - 要在字符串中划线的索引
      抛出:
      IllegalArgumentException - 如果index >= 文本长度,或 < -1
      自:
      1.4
    • getDisplayedMnemonicIndex

      public int getDisplayedMnemonicIndex()
      返回外观和感觉应提供装饰以表示助记键字符的字符索引。
      返回:
      表示助记键字符的索引
      自:
      1.4
      参见:
    • checkHorizontalKey

      protected int checkHorizontalKey(int key, String message)
      验证key是否是horizontalAlignment属性的合法值。
      参数:
      key - 要检查的属性值
      message - IllegalArgumentException详细消息
      返回:
      如果key是horizontalAlignment属性的合法值,则返回key值
      抛出:
      IllegalArgumentException - 如果key不是LEFT、CENTER、RIGHT、LEADING或TRAILING。
      参见:
    • checkVerticalKey

      protected int checkVerticalKey(int key, String message)
      验证key是否是verticalAlignment或verticalTextPosition属性的合法值。
      参数:
      key - 要检查的属性值
      message - IllegalArgumentException详细消息
      返回:
      如果key是verticalAlignment或verticalTextPosition属性的合法值,则返回key值
      抛出:
      IllegalArgumentException - 如果key不是TOP、CENTER或BOTTOM。
      参见:
    • getIconTextGap

      public int getIconTextGap()
      返回此标签中显示的文本和图标之间的间距。
      返回:
      一个等于文本和图标之间像素数的int值。
      参见:
    • setIconTextGap

      @BeanProperty(visualUpdate=true, description="If both the icon and text properties are set, this property defines the space between them.") public void setIconTextGap(int iconTextGap)
      如果同时设置了图标和文本属性,则此属性定义它们之间的间距。

      此属性的默认值为4像素。

      这是一个JavaBeans绑定属性。

      参数:
      iconTextGap - 图标和文本属性之间的间距
      参见:
    • getVerticalAlignment

      public int getVerticalAlignment()
      返回标签内容沿Y轴的对齐方式。
      返回:
      verticalAlignment属性的值,SwingConstants中定义的以下常量之一:TOPCENTERBOTTOM
      参见:
    • setVerticalAlignment

      @BeanProperty(visualUpdate=true, enumerationValues={"SwingConstants.TOP","SwingConstants.CENTER","SwingConstants.BOTTOM"}, description="The alignment of the label\'s contents along the Y axis.") public void setVerticalAlignment(int alignment)
      设置标签内容沿Y轴的对齐方式。

      此属性的默认值为CENTER。

      参数:
      alignment - SwingConstants中定义的以下常量之一:TOPCENTER(默认值)或BOTTOM
      参见:
    • getHorizontalAlignment

      public int getHorizontalAlignment()
      返回标签内容沿X轴的对齐方式。
      返回值:
      horizontalAlignment 属性的值,其中之一是在 SwingConstants 中定义的以下常量之一: LEFT, CENTER, RIGHT, LEADINGTRAILING
      参见:
    • setHorizontalAlignment

      @BeanProperty(visualUpdate=true, enumerationValues={"SwingConstants.LEFT","SwingConstants.CENTER","SwingConstants.RIGHT","SwingConstants.LEADING","SwingConstants.TRAILING"}, description="The alignment of the label\'s content along the X axis.") public void setHorizontalAlignment(int alignment)
      设置标签内容沿 X 轴的对齐方式。

      这是一个 JavaBeans 绑定属性。

      参数:
      alignment - 在 SwingConstants 中定义的以下常量之一: LEFT, CENTER(仅用于仅包含图像的标签的默认值), RIGHT, LEADING(仅用于仅包含文本的标签的默认值) 或 TRAILING
      参见:
    • getVerticalTextPosition

      public int getVerticalTextPosition()
      返回标签文本相对于其图像的垂直位置。
      返回值:
      SwingConstants 中定义的以下常量之一: TOP, CENTER, 或 BOTTOM
      参见:
    • setVerticalTextPosition

      @BeanProperty(expert=true, visualUpdate=true, enumerationValues={"SwingConstants.TOP","SwingConstants.CENTER","SwingConstants.BOTTOM"}, description="The vertical position of the text relative to it\'s image.") public void setVerticalTextPosition(int textPosition)
      设置标签文本相对于其图像的垂直位置。

      此属性的默认值为 CENTER。

      这是一个 JavaBeans 绑定属性。

      参数:
      textPosition - 在 SwingConstants 中定义的以下常量之一: TOP, CENTER(默认值), 或 BOTTOM
      参见:
    • getHorizontalTextPosition

      public int getHorizontalTextPosition()
      返回标签文本相对于其图像的水平位置。
      返回值:
      SwingConstants 中定义的以下常量之一: LEFT, CENTER, RIGHT, LEADINGTRAILING
      参见:
    • setHorizontalTextPosition

      @BeanProperty(expert=true, visualUpdate=true, enumerationValues={"SwingConstants.LEFT","SwingConstants.CENTER","SwingConstants.RIGHT","SwingConstants.LEADING","SwingConstants.TRAILING"}, description="The horizontal position of the label\'s text, relative to its image.") public void setHorizontalTextPosition(int textPosition)
      设置标签文本相对于其图像的水平位置。
      参数:
      textPosition - 在 SwingConstants 中定义的以下常量之一: LEFT, CENTER, RIGHT, LEADING, 或 TRAILING(默认值)。
      参见:
    • imageUpdate

      public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
      如果组件未显示或图标或禁用图标不是具有与传入的 Image 相等的 ImageImageIcon,则返回 false;否则将委托给超类。
      指定者:
      imageUpdate 在接口 ImageObserver
      覆盖:
      imageUpdate 在类 Component
      参数:
      img - 要比较的 Image
      infoflags - 在更新图像时用于重绘标签的标志,确定要绘制多少
      x - x 坐标
      y - y 坐标
      w - 宽度
      h - 高度
      返回值:
      如果 infoflags 指示图像已完全加载,则返回 false;否则返回 true
      参见:
    • paramString

      protected String paramString()
      返回此 JLabel 的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为 null
      覆盖:
      paramString 在类 JComponent
      返回值:
      此 JLabel 的字符串表示形式。
    • getLabelFor

      public Component getLabelFor()
      获取此标签化的组件。
      返回值:
      此标签化的组件。如果此标签未标记组件,则可以为 null。如果设置了 displayedMnemonic 属性并且也设置了 labelFor 属性,则在激活助记键时,标签将调用由 labelFor 属性指定的组件的 requestFocus 方法。
      参见:
    • setLabelFor

      @BeanProperty(description="The component this is labelling.") public void setLabelFor(Component c)
      设置此标签化的组件。如果此标签未标记组件,则可以为 null。如果设置了 displayedMnemonic 属性并且也设置了 labelFor 属性,则在激活助记键时,标签将调用由 labelFor 属性指定的组件的 requestFocus 方法。
      参数:
      c - 此标签所用于的组件,如果标签未标记组件,则为 null
      参见:
    • getAccessibleContext

      @BeanProperty(bound=false, expert=true, description="The AccessibleContext associated with this Label.") public AccessibleContext getAccessibleContext()
      获取此对象的 AccessibleContext
      指定者:
      getAccessibleContext 在接口 Accessible
      覆盖:
      getAccessibleContext 在类 Component
      返回值:
      此对象的 AccessibleContext