public class Button extends Component implements Accessible
Quit ”按钮的三个视图:

第一个视图显示按钮正常显示。 第二个视图显示按钮具有输入焦点。 它的轮廓变暗,让用户知道它是一个活动对象。 第三个视图显示当用户单击鼠标在按钮上方时的按钮,从而请求执行操作。
使用鼠标点击按钮的手势与ActionEvent的一个实例相关ActionEvent ,当一个按钮被按下并释放时,它被发送出去。 如果应用程序有兴趣知道按钮何时被按下但未被释放,作为单独的手势,它可以专门化processMouseEvent ,或者可以通过调用addMouseListener将其自身注册为鼠标事件的监听器。 这两种方法都由Component定义,所有组件的抽象超类。
当按下和释放按钮,AWT发送的一个实例ActionEvent到按钮,通过调用processEvent上的按钮。 按钮的processEvent方法接收按钮的所有事件; 它通过调用自己的processActionEvent方法传递一个动作事件。 后一种方法将动作事件传递给任何对该按钮生成的动作事件感兴趣的动作侦听器。
如果应用程序想按照按下并释放的按钮执行某些操作,则应通过调用按钮的addActionListener方法来实现ActionListener并注册新的监听器以接收此按钮的事件。 应用程序可以使用按钮的动作命令作为消息传递协议。
| Modifier and Type | Class and Description |
|---|---|
protected class |
Button.AccessibleAWTButton
这个类实现了可访问性支持
Button类。
|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategyaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
Button()
构造一个带有标签的空字符串的按钮。
|
Button(String label)
构造具有指定标签的按钮。
|
| Modifier and Type | Method and Description |
|---|---|
void |
addActionListener(ActionListener l)
添加指定的动作侦听器以从此按钮接收动作事件。
|
void |
addNotify()
创建按钮的对等体。
|
AccessibleContext |
getAccessibleContext()
获取
AccessibleContext与此相关
Button 。
|
String |
getActionCommand()
返回此按钮触发的操作事件的命令名称。
|
ActionListener[] |
getActionListeners()
返回在此按钮上注册的所有动作侦听器的数组。
|
String |
getLabel()
获取此按钮的标签。
|
<T extends EventListener> |
getListeners(类<T> listenerType)
返回当前注册为
FooListener的所有对象的数组,在此
Button 。
|
protected String |
paramString()
返回一个代表此
Button状态的字符串。
|
protected void |
processActionEvent(ActionEvent e)
通过将此按钮分派到任何已注册的
ActionListener对象来处理此按钮上发生的操作事件。
|
protected void |
processEvent(AWTEvent e)
处理此按钮上的事件。
|
void |
removeActionListener(ActionListener l)
删除指定的动作侦听器,使其不再从此按钮接收到动作事件。
|
void |
setActionCommand(String command)
设置此按钮触发的操作事件的命令名称。
|
void |
setLabel(String label)
将按钮的标签设置为指定的字符串。
|
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, 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, 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, validatepublic Button()
throws HeadlessException
HeadlessException - 如果GraphicsEnvironment.isHeadless()返回true
GraphicsEnvironment.isHeadless()
public Button(String label) throws HeadlessException
label - 按钮的字符串标签,或无标签的
null
HeadlessException - 如果GraphicsEnvironment.isHeadless()返回true
GraphicsEnvironment.isHeadless()
public void addNotify()
public String getLabel()
null 。
setLabel(java.lang.String)
public void setLabel(String label)
label - 新标签,或
null如果按钮没有标签。
getLabel()
public void setActionCommand(String command)
command - 用于设置按钮的动作命令的字符串。
如果字符串为null则将该命令设置为与该按钮的标签相匹配。
ActionEvent
public String getActionCommand()
null (默认),则此方法返回按钮的标签。
public void addActionListener(ActionListener l)
请参阅AWT Threading Issues有关AWT的线程模型的细节。
l - 动作侦听器
removeActionListener(java.awt.event.ActionListener) , getActionListeners() , ActionListener
public void removeActionListener(ActionListener l)
请参阅AWT Threading Issues有关AWT的线程模型的细节。
l - 动作侦听器
addActionListener(java.awt.event.ActionListener) , getActionListeners() , ActionListener
public ActionListener[] getActionListeners()
ActionListener s或一个空数组,如果没有动作侦听器当前注册
addActionListener(java.awt.event.ActionListener) , removeActionListener(java.awt.event.ActionListener) , ActionListener
public <T extends EventListener> T[] getListeners(类<T> listenerType)
FooListener的所有对象的数组,在此Button 。
FooListener使用addFooListener 。
您可以使用类文字指定listenerType参数,如FooListener.class 。 例如,您可以使用以下代码查询Button b其操作侦听器:
ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));
如果没有这样的侦听器存在,这个方法返回一个空数组。
getListeners在类别
Component
listenerType - 所请求的听众的类型;
此参数应指定从java.util.EventListener下降的java.util.EventListener
FooListener的所有对象的数组,如果没有添加此类侦听器,则为空数组
ClassCastException - 如果
listenerType未指定实现java.util.EventListener的类或
java.util.EventListener
getActionListeners()
protected void processEvent(AWTEvent e)
ActionEvent一个实例, ActionEvent此方法调用processActionEvent方法。
否则,它在processEvent上调用processEvent。
请注意,如果事件参数为null则该行为未指定,可能会导致异常。
processEvent在
Component
e - 事件
ActionEvent , processActionEvent(java.awt.event.ActionEvent)
protected void processActionEvent(ActionEvent e)
ActionListener对象来处理此按钮上发生的操作事件。
除非为此按钮启用了操作事件,否则不会调用此方法。 当发生以下情况之一时,启用操作事件:
ActionListener对象通过addActionListener 。 enableEvents启用操作事件。 请注意,如果事件参数为null则行为未指定,可能会导致异常。
e - 动作事件
ActionListener , addActionListener(java.awt.event.ActionListener) , Component.enableEvents(long)
protected String paramString()
Button状态的字符串。
该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。
返回的字符串可能为空,但可能不是null 。
paramString在类别
Component
public AccessibleContext getAccessibleContext()
AccessibleContext与此相关Button 。
对于按钮, AccessibleContext需要一个的形式AccessibleAWTButton 。
如果需要,将创建一个新的AccessibleAWTButton实例。
getAccessibleContext在界面
Accessible
getAccessibleContext在类别
Component
AccessibleAWTButton ,作为
AccessibleContext的这个
Button
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.