- 所有已实现的接口:
ImageObserver,MenuContainer,Serializable,Accessible,SwingConstants
- 已知子类:
BasicComboBoxRenderer,DefaultListCellRenderer,DefaultTableCellRenderer,DefaultTreeCellRenderer
JLabel 对象可以显示文本、图像或两者。您可以通过设置垂直和水平对齐方式来指定标签内容在标签显示区域中的对齐位置。默认情况下,标签在其显示区域中垂直居中。默认情况下,纯文本标签是前缘对齐的;默认情况下,仅图像标签水平居中。
您还可以指定文本相对于图像的位置。默认情况下,文本位于图像的后缘,文本和图像垂直对齐。
标签的前缘和后缘由其 ComponentOrientation 属性的值确定。目前,默认的 ComponentOrientation 设置将前缘映射到左侧,将后缘映射到右侧。
最后,您可以使用 setIconTextGap 方法指定文本和图像之间应出现多少像素。默认值为 4 个像素。
有关更多文档,请参阅 The Java Tutorial 中的 如何使用标签。
Warning: Swing 不是线程安全的。有关详细信息,请参阅 Swing 的线程策略。
Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder 。
- 自从:
- 1.2
-
内部类总结
内部类在类 javax.swing.JComponent 中声明的嵌套类/接口
JComponent.AccessibleJComponent在类 java.awt.Container 中声明的嵌套类/接口
Container.AccessibleAWTContainer -
字段摘要
字段在类 javax.swing.JComponent 中声明的字段
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW在类 java.awt.Component 中声明的字段
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT在接口 java.awt.image.ImageObserver 中声明的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH在接口 javax.swing.SwingConstants 中声明的字段
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST -
构造方法总结
构造方法构造方法描述JLabel()创建一个没有图像且标题为空字符串的JLabel实例。使用指定的文本创建一个JLabel实例。创建具有指定文本和水平对齐方式的JLabel实例。创建具有指定文本、图像和水平对齐方式的JLabel实例。使用指定图像创建一个JLabel实例。创建具有指定图像和水平对齐方式的JLabel实例。 -
方法总结
修饰符和类型方法描述protected intcheckHorizontalKey(int key, String message) 验证 key 是 horizontalAlignment 属性的合法值。protected intcheckVerticalKey(int key, String message) 验证 key 是 verticalAlignment 或 verticalTextPosition 属性的合法值。获取此对象的 AccessibleContext返回标签在禁用时使用的图标。int返回表示助记键的键码。int返回作为索引的字符,外观应该为其提供装饰以表示助记字符。int返回标签内容沿 X 轴的对齐方式。int返回标签文本相对于其图像的水平位置。getIcon()返回标签显示的图形图像(字形、图标)。int返回此标签中显示的文本和图标之间的间距量。获取正在标记的组件。getText()返回标签显示的文本字符串。getUI()返回呈现此组件的 L&F 对象。返回一个字符串,该字符串指定呈现此组件的 l&f 类的名称。int返回标签内容沿 Y 轴的对齐方式。int返回标签文本相对于其图像的垂直位置。booleanimageUpdate(Image img, int infoflags, int x, int y, int w, int h) 如果组件未显示或者图标或禁用图标不是ImageIcon且Image等于传入的Image,则返回false;否则它将委托给超类。protected String返回此 JLabel 的字符串表示形式。voidsetDisabledIcon(Icon disabledIcon) 如果此 JLabel 被“禁用”(JLabel.setEnabled(false)),则设置要显示的图标。voidsetDisplayedMnemonic(char aChar) 将显示的助记符指定为字符值。voidsetDisplayedMnemonic(int key) 指定表示助记键的键码。voidsetDisplayedMnemonicIndex(int index) 提供有关应修饰文本中的哪个字符以表示助记符的外观提示。voidsetHorizontalAlignment(int alignment) 设置标签内容沿 X 轴的对齐方式。voidsetHorizontalTextPosition(int textPosition) 设置标签文本相对于其图像的水平位置。void定义此组件将显示的图标。voidsetIconTextGap(int iconTextGap) 如果同时设置了 icon 和 text 属性,则此属性定义它们之间的空间。void设置这是标签的组件。void定义此组件将显示的单行文本。void设置呈现此组件的 L&F 对象。voidsetVerticalAlignment(int alignment) 设置标签内容沿 Y 轴的对齐方式。voidsetVerticalTextPosition(int textPosition) 设置标签文本相对于其图像的垂直位置。voidupdateUI()将 UI 属性重置为当前外观的值。在类 javax.swing.JComponent 中声明的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update在类 java.awt.Container 中声明的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree在类 java.awt.Component 中声明的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
字段详细信息
-
labelFor
该标签适用于的组件;如果标签不是组件的标签,则为 null
-
-
构造方法详细信息
-
JLabel
创建具有指定文本、图像和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。文本位于图像的后缘。- 参数:
text- 标签要显示的文本。icon- 标签要显示的图像。horizontalAlignment-SwingConstants中定义的以下常量之一:LEFT、CENTER、RIGHT、LEADING或TRAILING。
-
JLabel
创建具有指定文本和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。- 参数:
text- 标签要显示的文本。horizontalAlignment-SwingConstants中定义的以下常量之一:LEFT、CENTER、RIGHT、LEADING或TRAILING。
-
JLabel
使用指定的文本创建一个JLabel实例。标签与其显示区域的前缘对齐,并垂直居中。- 参数:
text- 标签要显示的文本。
-
JLabel
创建具有指定图像和水平对齐方式的JLabel实例。标签在其显示区域中垂直居中。- 参数:
image- 标签要显示的图像。horizontalAlignment-SwingConstants中定义的以下常量之一:LEFT、CENTER、RIGHT、LEADING或TRAILING。
-
JLabel
使用指定图像创建一个JLabel实例。标签在其显示区域中垂直和水平居中。- 参数:
image- 标签要显示的图像。
-
JLabel
public JLabel()创建一个没有图像且标题为空字符串的JLabel实例。标签在其显示区域中垂直居中。标签的内容一旦设置,将显示在标签显示区域的前缘。
-
-
方法详情
-
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
返回一个字符串,该字符串指定呈现此组件的 l&f 类的名称。- 重写:
getUIClassID在类JComponent中- 返回:
- 字符串“LabelUI”
- 参见:
-
getText
返回标签显示的文本字符串。- 返回:
- 一个字符串
- 参见:
-
setText
@BeanProperty (preferred =true, visualUpdate =true, description ="Defines the single line of text this component will display.") public void setText(String text) 定义此组件将显示的单行文本。如果 text 的值为 null 或空字符串,则不显示任何内容。此属性的默认值为 null。
这是一个 JavaBeans 绑定属性。
- 参数:
text- 该组件将显示的单行文本- 参见:
-
getIcon
返回标签显示的图形图像(字形、图标)。- 返回:
- 一个图标
- 参见:
-
setIcon
@BeanProperty (preferred =true, visualUpdate =true, description ="The icon this component will display.") public void setIcon(Icon icon) 定义此组件将显示的图标。如果 icon 的值为 null,则不显示任何内容。此属性的默认值为 null。
这是一个 JavaBeans 绑定属性。
- 参数:
icon- 该组件将显示的默认图标- 参见:
-
getDisabledIcon
返回标签在禁用时使用的图标。如果未设置禁用图标,这会将调用转发给外观以构建适当的禁用图标。某些外观可能不会呈现禁用的图标,在这种情况下,它们将忽略它。
- 返回:
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) 将显示的助记符指定为字符值。- 参数:
aChar- 一个字符,指定要显示的助记符- 参见:
-
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 表示没有助记词,字符串中不包含助记符字符,或者开发人员不希望显示助记词。this 的值随着与助记符相关的属性(例如助记符本身、文本...)的变化而更新。如果您不希望为默认字符加下划线,则只应调用此方法。例如,如果文本为“另存为”,助记符为“a”,而您希望修饰“A”,如“保存”As',您必须在调用
setDisplayedMnemonic(KeyEvent.VK_A)之后调用setDisplayedMnemonicIndex(5)。- 参数:
index- 要下划线的字符串索引- 抛出:
IllegalArgumentException- 如果index>= 文本长度,或者 < -1,将被抛出- 自从:
- 1.4
-
getDisplayedMnemonicIndex
public int getDisplayedMnemonicIndex()返回作为索引的字符,外观应该为其提供装饰以表示助记字符。- 返回:
- 代表助记符的索引
- 自从:
- 1.4
- 参见:
-
checkHorizontalKey
验证 key 是 horizontalAlignment 属性的合法值。- 参数:
key- 要检查的属性值message- IllegalArgumentException 详细消息- 返回:
-
如果
key是 horizontalAlignment 属性的合法值,则为键值 - 抛出:
IllegalArgumentException- 如果键不是 LEFT、CENTER、RIGHT、LEADING 或 TRAILING。- 参见:
-
checkVerticalKey
验证 key 是 verticalAlignment 或 verticalTextPosition 属性的合法值。- 参数:
key- 要检查的属性值message- IllegalArgumentException 详细消息- 返回:
-
如果
key是 verticalAlignment 或 verticalTextPosition 属性的合法值,则为键值 - 抛出:
IllegalArgumentException- 如果键不是 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) 如果同时设置了 icon 和 text 属性,则此属性定义它们之间的空间。此属性的默认值为 4 像素。
这是一个 JavaBeans 绑定属性。
- 参数:
iconTextGap- 图标和文本属性之间的空间- 参见:
-
getVerticalAlignment
public int getVerticalAlignment()返回标签内容沿 Y 轴的对齐方式。- 返回:
-
verticalAlignment 属性的值,是
SwingConstants中定义的以下常量之一:TOP、CENTER或BOTTOM。 - 参见:
-
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中定义的以下常量之一:TOP、CENTER(默认值)或BOTTOM。- 参见:
-
getHorizontalAlignment
public int getHorizontalAlignment()返回标签内容沿 X 轴的对齐方式。- 返回:
-
horizontalAlignment 属性的值,是
SwingConstants中定义的以下常量之一:LEFT、CENTER、RIGHT、LEADING或TRAILING。 - 参见:
-
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、LEADING或TRAILING。- 参见:
-
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
如果组件未显示或者图标或禁用图标不是ImageIcon且Image等于传入的Image,则返回false;否则它将委托给超类。- 指定者:
imageUpdate在接口ImageObserver中- 重写:
imageUpdate在类Component中- 参数:
img- 要比较的Imageinfoflags- 用于在更新图像时重新绘制标签并确定要绘制多少的标志x- x 坐标y- y 坐标w- 宽度h- 高度- 返回:
false如果信息标志表明图像已完全加载;true否则。- 参见:
-
paramString
返回此 JLabel 的字符串表示形式。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是null。- 重写:
paramString在类JComponent中- 返回:
- 此 JLabel 的字符串表示形式。
-
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
-