public class JLabel extends JComponent implements SwingConstants, Accessible
 JLabel对象可以显示文本,图像或两者。 您可以通过设置垂直和水平对齐来指定标签的显示区域中标签内容对齐的位置。 默认情况下,标签在其显示区域中垂直居中。 默认情况下,纯文本标签是前缘对齐的; 默认情况下,仅图像标签水平居中。 
您还可以指定文本相对于图像的位置。 默认情况下,文本位于图像的后端,文本和图像垂直对齐。
 标签的前沿和后沿由其ComponentOrientation属性的值确定。 目前,默认的ComponentOrientation设置将前缘向左和后边缘对齐。 
 最后,您可以使用setIconTextGap方法来指定文本和图像之间应显示多少像素。 默认值为4像素。 
见How to Use Labels在Java教程进一步文档。
警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy 。
 警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,支持所有JavaBeans的长期存储已经添加到java.beans包中。 请参阅XMLEncoder 。 
| Modifier and Type | Class and Description | 
|---|---|
| protected class  | JLabel.AccessibleJLabel
              该类用于获取此对象的可访问角色。 
             | 
JComponent.AccessibleJComponentContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy| Modifier and Type | Field and Description | 
|---|---|
| protected Component | labelFor | 
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTBOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WESTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description | 
|---|
| JLabel()
              创建一个没有图像的 
              JLabel实例,标题为空字符串。 | 
| JLabel(Icon image)
              使用指定的图像创建一个 
              JLabel实例。 | 
| JLabel(Icon image, int horizontalAlignment)
              创建一个具有指定图像和水平对齐的 
              JLabel实例。 | 
| JLabel(String text)
              使用指定的文本创建一个 
              JLabel实例。 | 
| JLabel(String text, Icon icon, int horizontalAlignment)
              创建具有 
              JLabel文本,图像和水平对齐的JLabel实例。 | 
| JLabel(String text, int horizontalAlignment)
              创建一个具有指定文本和水平对齐的 
              JLabel实例。 | 
| Modifier and Type | Method and Description | 
|---|---|
| protected int | checkHorizontalKey(int key, String message)
              验证该键是horizontalAlignment属性的合法值。 
             | 
| protected int | checkVerticalKey(int key, String message)
              验证该键是verticalAlignment或verticalTextPosition属性的合法值。 
             | 
| AccessibleContext | getAccessibleContext()
              获取此对象的AccessibleContext 
             | 
| Icon | getDisabledIcon()
              返回标签禁用时使用的图标。 
             | 
| int | getDisplayedMnemonic()
              返回指示助记键的键码。 
             | 
| int | getDisplayedMnemonicIndex()
              返回字符,作为索引,外观和感觉应提供装饰用于表示助记符。 
             | 
| int | getHorizontalAlignment()
              返回标签内容沿X轴的对齐方式。 
             | 
| int | getHorizontalTextPosition()
              返回标签相对于其图像的文本的水平位置。 
             | 
| Icon | getIcon()
              返回标签显示的图形图像(字形,图标)。 
             | 
| int | getIconTextGap()
              返回文本与此标签中显示的图标之间的空间量。 
             | 
| Component | getLabelFor()
              获取此标签的组件。 
             | 
| String | getText()
              返回标签显示的文本字符串。 
             | 
| LabelUI | getUI()
              返回渲染此组件的L&F对象。 
             | 
| String | getUIClassID()
              返回一个字符串,指定呈现此组件的l&f类的名称。 
             | 
| int | getVerticalAlignment()
              返回标签内容沿着Y轴的对齐方式。 
             | 
| int | getVerticalTextPosition()
              返回标签相对于其图像的文本的垂直位置。 
             | 
| boolean | imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
              如果当前图标的图像不等于图像 
              img传递的图像,则这被覆盖以返回false。 | 
| protected String | paramString()
              返回该JLabel的字符串表示形式。 
             | 
| void | setDisabledIcon(Icon disabledIcon)
              如果此JLabel为“禁用”(JLabel.setEnabled(false)),请设置要显示的图标。 
             | 
| void | setDisplayedMnemonic(char aChar)
              指定显示的助记符为char值。 
             | 
| void | setDisplayedMnemonic(int key)
              指定一个指示助记键的键码。 
             | 
| void | setDisplayedMnemonicIndex(int index)
              提供关于文字中哪个字符应该被装饰以表示助记符的外观和感觉的提示。 
             | 
| void | setHorizontalAlignment(int alignment)
              设置标签内容沿着X轴的对齐方式。 
             | 
| void | setHorizontalTextPosition(int textPosition)
              设置标签文本相对于其图像的水平位置。 
             | 
| void | setIcon(Icon icon)
              定义该组件将显示的图标。 
             | 
| void | setIconTextGap(int iconTextGap)
              如果设置了图标和文本属性,则此属性定义它们之间的空格。 
             | 
| void | setLabelFor(Component c)
              设置标签的组件。 
             | 
| void | setText(String text)
              定义该组件将显示的单行文本。 
             | 
| void | setUI(LabelUI ui)
              设置渲染此组件的L&F对象。 
             | 
| void | setVerticalAlignment(int alignment)
              设置标签内容沿着Y轴的对齐方式。 
             | 
| void | setVerticalTextPosition(int textPosition)
              设置标签文本相对于其图像的垂直位置。 
             | 
| void | updateUI()
              将UI属性重置为当前外观的值。 
             | 
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, 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, updateadd, 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, validateTreeaction, 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, 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, getPeer, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycleprotected Component labelFor
public JLabel(String text, Icon icon, int horizontalAlignment)
JLabel文本,图像和水平对齐的JLabel实例。 
           标签在其显示区域中垂直居中。 
           该文本位于图像的后端。 
          text - 标签显示的文字。 
           icon - 要由标签显示的图像。 
           horizontalAlignment -一个中所定义的以下常量 
            SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING或 
            TRAILING 。 
           public JLabel(String text, int horizontalAlignment)
JLabel文本和水平对齐的JLabel实例。 
           标签在其显示区域中垂直居中。 
          text - 标签显示的文字。 
           horizontalAlignment -一个中所定义的以下常量 
            SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING或 
            TRAILING 。 
           public JLabel(String text)
JLabel实例。 
           标签对准其显示区域的前缘,并垂直居中。 
          text - 要由标签显示的文字。 
           public JLabel(Icon image, int horizontalAlignment)
JLabel图像和水平对齐的JLabel实例。 
           标签在其显示区域中垂直居中。 
          image - 要由标签显示的图像。 
           horizontalAlignment -一个中所定义的以下常量 
            SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING或 
            TRAILING 。 
           public JLabel(Icon image)
JLabel实例。 
           标签在其显示区域中垂直和水平居中。 
          image - 要由标签显示的图像。 
           public JLabel()
JLabel实例,并为标题创建一个空字符串。 
           标签在其显示区域中垂直居中。 
           标签的内容一旦设置,将显示在标签的显示区域的前沿。 
          public LabelUI getUI()
public void setUI(LabelUI ui)
ui - LabelUI L&F对象 
           UIDefaults.getUI(javax.swing.JComponent) 
           public void updateUI()
updateUI在 
            JComponent 
           JComponent.updateUI() 
           public String getUIClassID()
getUIClassID在类 
            JComponent 
           JComponent.getUIClassID() , 
            UIDefaults.getUI(javax.swing.JComponent) 
           public String getText()
setText(java.lang.String) 
           public void setText(String text)
此属性的默认值为null。
这是一个JavaBeans绑定属性。
public Icon getIcon()
setIcon(javax.swing.Icon) 
           public void setIcon(Icon icon)
此属性的默认值为null。
这是一个JavaBeans绑定属性。
public Icon getDisabledIcon()
有些看起来和感觉可能不会渲染残疾人图标,在这种情况下,他们将忽略这一点。
disabledIcon属性 
           setDisabledIcon(javax.swing.Icon) , LookAndFeel.getDisabledIcon(javax.swing.JComponent, javax.swing.Icon) , ImageIcon 
           public void setDisabledIcon(Icon disabledIcon)
此属性的默认值为null。
disabledIcon - 组件被禁用时显示的图标 
           getDisabledIcon() , 
            JComponent.setEnabled(boolean) 
           public void setDisplayedMnemonic(int key)
public void setDisplayedMnemonic(char aChar)
aChar - 一个char指定要显示的助记符 
           setDisplayedMnemonic(int) 
           public int getDisplayedMnemonic()
getLabelFor() , 
            setLabelFor(java.awt.Component) 
           public void setDisplayedMnemonicIndex(int index)
                               throws IllegalArgumentException 
           这个值被更新为与助记符变化相关的属性(如助记符本身,文本...)。 如果您不希望默认字符被加下划线,您应该只需要调用它。 例如,如果文本是“另存为”,用“A”的记忆,和你想要的“A”加以装饰,为“ 节省 S”,你就必须调用setDisplayedMnemonicIndex(5)调用后setDisplayedMnemonic(KeyEvent.VK_A) 。 
index - 索引到字符串下划线 
           IllegalArgumentException - 如果 
            index是> =文本的长度,或者<-1 
           public int getDisplayedMnemonicIndex()
setDisplayedMnemonicIndex(int) 
           protected int checkHorizontalKey(int key,
                                 String message) 
          key - 要检查的属性值 
           message - IllegalArgumentException详细消息 
           IllegalArgumentException - 如果键不是左,中,右,引导或跟踪。 
           setHorizontalTextPosition(int) , 
            setHorizontalAlignment(int) 
           protected int checkVerticalKey(int key,
                               String message) 
          key - 要检查的属性值 
           message - IllegalArgumentException详细信息 
           IllegalArgumentException - 如果键不是TOP,CENTER或BOTTOM。 
           setVerticalAlignment(int) , 
            setVerticalTextPosition(int) 
           public int getIconTextGap()
setIconTextGap(int) 
           public void setIconTextGap(int iconTextGap)
此属性的默认值为4像素。
这是一个JavaBeans绑定属性。
getIconTextGap() 
           public int getVerticalAlignment()
SwingConstants : 
            TOP , 
            CENTER ,或 
            BOTTOM 。 
           SwingConstants , setVerticalAlignment(int) 
           public void setVerticalAlignment(int alignment)
此属性的默认值为CENTER。
alignment -一个中所定义的以下常量 
            SwingConstants : 
            TOP , 
            CENTER (缺省值),或 
            BOTTOM 。 
           SwingConstants , getVerticalAlignment() 
           public int getHorizontalAlignment()
SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING或 
            TRAILING 。 
           setHorizontalAlignment(int) , SwingConstants 
           public void setHorizontalAlignment(int alignment)
这是一个JavaBeans绑定属性。
alignment -一个中所定义的以下常量 
            SwingConstants : 
            LEFT , 
            CENTER (仅用于图像的标签的默认值), 
            RIGHT , 
            LEADING (默认为纯文本的标记),或 
            TRAILING 。 
           SwingConstants , getHorizontalAlignment() 
           public int getVerticalTextPosition()
SwingConstants : 
            TOP , 
            CENTER ,或 
            BOTTOM 。 
           setVerticalTextPosition(int) , SwingConstants 
           public void setVerticalTextPosition(int textPosition)
此属性的默认值为CENTER。
这是一个JavaBeans绑定属性。
textPosition -一个中所定义的以下常量 
            SwingConstants : 
            TOP , 
            CENTER (缺省值),或 
            BOTTOM 。 
           SwingConstants , getVerticalTextPosition() 
           public int getHorizontalTextPosition()
SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING或 
            TRAILING 。 
           SwingConstants 
           public void setHorizontalTextPosition(int textPosition)
textPosition -一个中所定义的以下常量 
            SwingConstants : 
            LEFT , 
            CENTER , 
            RIGHT , 
            LEADING ,或 
            TRAILING (缺省值)。 
           IllegalArgumentException 
           SwingConstants 
           public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
img传递的图像,则这被覆盖以返回false。 
          imageUpdate在接口 
            ImageObserver 
           imageUpdate在 
            Component 
           img - 正在观察的图像 
           infoflags -见 
            imageUpdate了解更多信息 
           x - 
            x坐标 
           y - 
            y坐标 
           w - 宽度 
           h - 高度 
           false如果infoflags指示图像被完全加载; 
            true否则。 
           ImageObserver , Component.imageUpdate(java.awt.Image, int, int, int, int, int) 
           protected String paramString()
null 。 
          paramString在 
            JComponent 
           public Component getLabelFor()
getDisplayedMnemonic() , 
            setDisplayedMnemonic(int) 
           public void setLabelFor(Component c)
c - 此标签所用的组件,如果标签不是组件的标签,则为null 
           getDisplayedMnemonic() , 
            setDisplayedMnemonic(int) 
           public AccessibleContext getAccessibleContext()
getAccessibleContext在接口 
            Accessible 
           getAccessibleContext在 
            Component 
            Submit a bug or feature 
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
 Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.