- 所有已实现的接口:
-
ImageObserver,MenuContainer,Serializable,Accessible
Quit”按钮的三个视图:

第一个视图显示按钮的正常外观。第二个视图显示当按钮获得输入焦点时的外观。其轮廓会变暗,以让用户知道它是一个活动对象。第三个视图显示当用户在按钮上单击鼠标时,从而请求执行某个操作。
用鼠标单击按钮的手势与一个 ActionEvent 实例相关联,当鼠标在按钮上同时按下并释放时,会发送出该事件。如果应用程序想要知道按钮何时被按下但未释放,作为一个单独的手势,它可以专门化 processMouseEvent,或者通过调用 addMouseListener 将自身注册为鼠标事件的监听器。这两种方法都是由 Component 定义的,它是所有组件的抽象超类。
当按钮被按下并释放时,AWT 通过调用按钮的 processEvent 向按钮发送一个 ActionEvent 实例。按钮的 processEvent 方法接收按钮的所有事件;它通过调用自己的 processActionEvent 方法将一个动作事件传递下去。后者将动作事件传递给任何已注册对此按钮生成的动作事件感兴趣的动作监听器。
如果应用程序想要根据按钮被按下并释放执行某些操作,应该实现 ActionListener 并通过调用按钮的 addActionListener 方法注册新的监听器以接收来自该按钮的事件。应用程序可以利用按钮的动作命令作为消息协议。
- 自 JDK 版本:
- 1.0
- 参见:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class此类为Button类实现了辅助功能支持。Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid添加指定的动作监听器以接收来自此按钮的动作事件。void创建按钮的对等体。获取与此Button关联的AccessibleContext。返回由此按钮触发的动作事件的命令名称。返回在此按钮上注册的所有动作监听器的数组。getLabel()获取此按钮的标签。<T extends EventListener>
T[]getListeners(Class<T> listenerType) 返回当前在此Button上注册为FooListener的所有对象的数组。protected String返回表示此Button状态的字符串。protected void通过将其分派给任何已注册对此按钮生成的动作事件感兴趣的ActionListener对象,处理在此按钮上发生的动作事件。protected void处理此按钮上的事件。void移除指定的动作监听器,使其不再接收来自此按钮的动作事件。voidsetActionCommand(String command) 设置此按钮触发的动作事件的命令名称。void将按钮的标签设置为指定的字符串。Methods declared in class java.awt.Component
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, 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, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
-
Constructor Details
-
Button
用空字符串构造一个按钮的实例。- 抛出:
-
HeadlessException- 如果 GraphicsEnvironment.isHeadless() 返回 true - 参见:
-
Button
用指定的标签构造一个按钮的实例。- 参数:
-
label- 按钮的字符串标签,或null表示无标签 - 抛出:
-
HeadlessException- 如果 GraphicsEnvironment.isHeadless() 返回 true - 参见:
-
-
Method Details
-
addNotify
public void addNotify()创建按钮的对等体。按钮的对等体允许应用程序更改按钮的外观而不更改其功能。 -
getLabel
获取此按钮的标签。- 返回:
-
按钮的标签,如果按钮没有标签则返回
null。 - 参见:
-
setLabel
将按钮的标签设置为指定的字符串。- 参数:
-
label- 新标签,如果按钮没有标签则为null。 - 参见:
-
setActionCommand
设置此按钮触发的动作事件的命令名称。默认情况下,此动作命令设置为与按钮的标签匹配。- 参数:
-
command- 用于设置按钮的动作命令的字符串。如果字符串为null,则动作命令设置为与按钮的标签匹配。 - 自 JDK 版本:
- 1.1
- 参见:
-
getActionCommand
返回此按钮触发的动作事件的命令名称。如果命令名称为null(默认值),则此方法返回按钮的标签。- 返回:
- 此按钮的动作命令名称(或标签)
-
addActionListener
添加指定的动作监听器以接收来自此按钮的动作事件。当用户在此按钮上按下或释放鼠标时,会触发动作事件。如果 l 为 null,则不会抛出异常且不会执行任何操作。有关 AWT 线程模型的详细信息,请参阅 AWT 线程问题。
- 参数:
-
l- 动作监听器 - 自 JDK 版本:
- 1.1
- 参见:
-
removeActionListener
移除指定的动作监听器,使其不再接收来自此按钮的动作事件。当用户在此按钮上按下或释放鼠标时,会触发动作事件。如果 l 为 null,则不会抛出异常且不会执行任何操作。有关 AWT 线程模型的详细信息,请参阅 AWT 线程问题。
- 参数:
-
l- 动作监听器 - 自 JDK 版本:
- 1.1
- 参见:
-
getActionListeners
返回在此按钮上注册的所有动作监听器的数组。- 返回:
-
所有此按钮的
ActionListener,如果当前没有注册任何动作监听器,则返回空数组 - 自 JDK 版本:
- 1.4
- 参见:
-
getListeners
返回当前在此Button上注册为FooListener的所有对象的数组。使用addFooListener方法注册FooListener。您可以使用类字面量指定
listenerType参数,例如FooListener.class。例如,您可以使用以下代码查询Button b的动作监听器:ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));
如果不存在此类监听器,则此方法返回一个空数组。- 覆盖:
-
getListeners在类Component - 类型参数:
-
T- 监听器的类型 - 参数:
-
listenerType- 请求的监听器类型;此参数应指定一个继承自java.util.EventListener的接口 - 返回:
-
返回在此按钮上注册为
FooListener的所有对象数组,如果没有添加此类监听器,则返回空数组 - 抛出:
-
ClassCastException- 如果listenerType未指定实现java.util.EventListener的类或接口 - 自版本:
- 1.3
- 另请参阅:
-
processEvent
处理此按钮上的事件。如果事件是ActionEvent的实例,则此方法调用processActionEvent方法。否则,它在超类上调用processEvent方法。请注意,如果事件参数为
null,则行为是未指定的,可能会导致异常。- 覆盖:
-
processEvent在类Component - 参数:
-
e- 事件 - 自版本:
- 1.1
- 另请参阅:
-
processActionEvent
通过将动作事件分派给任何已注册的ActionListener对象来处理此按钮上发生的动作事件。除非为此按钮启用了动作事件,否则不会调用此方法。当发生以下情况之一时,将启用动作事件:
- 通过
addActionListener注册了一个ActionListener对象。 - 通过
enableEvents启用了动作事件。
请注意,如果事件参数为
null,则行为是未指定的,可能会导致异常。- 参数:
-
e- 动作事件 - 自版本:
- 1.1
- 另请参阅:
- 通过
-
paramString
返回表示此Button状态的字符串。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null。- 覆盖:
-
paramString在类Component - 返回:
- 此按钮的参数字符串
-
getAccessibleContext
@BeanProperty(expert=true, description="The AccessibleContext associated with this Button.") public AccessibleContext getAccessibleContext()获取与此Button关联的AccessibleContext。对于按钮,AccessibleContext采用AccessibleAWTButton的形式。如有必要,将创建一个新的AccessibleAWTButton实例。- 指定者:
-
getAccessibleContext在接口Accessible - 覆盖:
-
getAccessibleContext在类Component - 返回:
-
作为此
Button的AccessibleContext的AccessibleAWTButton - 自版本:
- 1.3
-