public class Label extends Component implements Accessible
Label
对象是用于将文本放置在容器中的组件。
标签显示一行只读文本。
文本可以由应用程序更改,但用户无法直接编辑。
例如,代码。 。 。
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); add(new Label("Hi There!")); add(new Label("Another Label"));
生成以下标签:
Modifier and Type | Class and Description |
---|---|
protected class |
Label.AccessibleAWTLabel
这个类实现了可访问性支持
Label 类。
|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static int |
CENTER
表示标签应该居中。
|
static int |
LEFT
表示标签应该保持对齐。
|
static int |
RIGHT
表示标签应该是正确的。
|
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
Label()
构造一个空标签。
|
Label(String text)
用指定的文本字符串构造一个新的标签,左对齐。
|
Label(String text, int alignment)
构造一个新的标签,以指定的对齐方式显示指定的文本字符串。
|
Modifier and Type | Method and Description |
---|---|
void |
addNotify()
创建此标签的对等体。
|
AccessibleContext |
getAccessibleContext()
获取与此Label相关联的AccessibleContext。
|
int |
getAlignment()
获取此标签的当前对齐方式。
|
String |
getText()
获取此标签的文本。
|
protected String |
paramString()
返回表示此
Label 的状态的字符串。
|
void |
setAlignment(int alignment)
将此标签的对齐方式设置为指定的对齐方式。
|
void |
setText(String text)
将此标签的文本设置为指定的文本。
|
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
public static final int LEFT
public static final int CENTER
public static final int RIGHT
public Label() throws HeadlessException
""
。
HeadlessException
- 如果GraphicsEnvironment.isHeadless()返回true。
GraphicsEnvironment.isHeadless()
public Label(String text) throws HeadlessException
text
- 标签显示的字符串。
将接受一个null
值,而不会导致抛出NullPointerException。
HeadlessException
- 如果GraphicsEnvironment.isHeadless()返回true。
GraphicsEnvironment.isHeadless()
public Label(String text, int alignment) throws HeadlessException
alignment
是Label.LEFT
, Label.RIGHT
和Label.CENTER
。
text
- 标签显示的字符串。
null
值将被接受,而不会导致抛出NullPointerException。
alignment
- 对齐值。
HeadlessException
- 如果GraphicsEnvironment.isHeadless()返回true。
GraphicsEnvironment.isHeadless()
public void addNotify()
public int getAlignment()
Label.LEFT
, Label.RIGHT
和Label.CENTER
。
setAlignment(int)
public void setAlignment(int alignment)
Label.LEFT
, Label.RIGHT
和Label.CENTER
。
alignment
- 要设置的对齐方式。
IllegalArgumentException
- 如果给出了
alignment
的不正确值。
getAlignment()
public String getText()
null
如果文本已设置为
null
。
setText(java.lang.String)
public void setText(String text)
text
- 此标签显示的文本。
如果text
是null
,它被视为显示目的,如空字符串""
。
getText()
protected String paramString()
Label
的状态的字符串。
该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。
返回的字符串可能为空,但可能不是null
。
paramString
在
Component
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.