- java.lang.Object
-
- java.awt.Component
-
- java.awt.Button
-
- All Implemented Interfaces:
-
ImageObserver,MenuContainer,Serializable,Accessible
public class Button extends Component implements Accessible
这个类创建一个标记的按钮。 当按下按钮时,应用程序可能会导致一些动作发生。 此图像描述了Solaris操作系统下的“Quit”按钮的三个视图:
第一个视图显示按钮正常显示。 第二个视图显示按钮具有输入焦点。 它的轮廓变暗,让用户知道它是一个活动对象。 第三个视图显示当用户单击鼠标在按钮上方时的按钮,从而请求执行操作。
使用鼠标点击按钮的手势与
ActionEvent一个实例相关联,当鼠标按下并释放按钮时,该实例被发送出去。 如果应用程序有兴趣知道按钮何时被按下但未被释放,作为单独的手势,它可以专门化processMouseEvent,或者可以通过调用addMouseListener将自己注册为鼠标事件的监听addMouseListener。 这两种方法都由Component定义,所有组件的抽象超类。当按下和释放按钮,AWT发送的一个实例
ActionEvent到按钮,通过调用processEvent上的按钮。 按钮的processEvent方法接收按钮的所有事件; 它通过调用自己的processActionEvent方法传递一个动作事件。 后一种方法将动作事件传递给任何对该按钮生成的动作事件感兴趣的动作侦听器。如果应用程序想按照按下并释放的按钮执行某些操作,则应通过调用按钮的
addActionListener方法来实现ActionListener并注册新的监听器以从该按钮接收事件。 应用程序可以使用按钮的动作命令作为消息传递协议。
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 protected classButton.AccessibleAWTButton这个类实现Button类的辅助功能支持。-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidaddActionListener(ActionListener l)添加指定的动作侦听器以从此按钮接收动作事件。voidaddNotify()创建按钮的对等体。AccessibleContextgetAccessibleContext()获取AccessibleContext与此Button相关联。StringgetActionCommand()返回此按钮触发的操作事件的命令名称。ActionListener[]getActionListeners()返回在此按钮上注册的所有动作侦听器的数组。StringgetLabel()获取此按钮的标签。<T extends EventListener>
T[]getListeners(Class<T> listenerType)返回此Button当前注册为FooListener的所有对象的数组。protected StringparamString()返回一个表示Button状态的字符串。protected voidprocessActionEvent(ActionEvent e)通过将此按钮分派到任何已注册的ActionListener对象来处理此按钮上发生的操作事件。protected voidprocessEvent(AWTEvent e)处理此按钮上的事件。voidremoveActionListener(ActionListener l)删除指定的动作侦听器,使其不再从此按钮接收到动作事件。voidsetActionCommand(String command)设置此按钮触发的操作事件的命令名称。voidsetLabel(String label)将按钮的标签设置为指定的字符串。-
Methods inherited from 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
-
-
-
-
构造方法详细信息
-
Button
public Button() throws HeadlessException构造一个带有标签的空字符串的按钮。- 异常
-
HeadlessException- 如果GraphicsEnvironment.isHeadless()返回true - 另请参见:
-
GraphicsEnvironment.isHeadless()
-
Button
public Button(String label) throws HeadlessException
构造具有指定标签的按钮。- 参数
-
label- 按钮的字符串标签,或无标签的null - 异常
-
如果GraphicsEnvironment.isHeadless()返回true,
HeadlessException - 另请参见:
-
GraphicsEnvironment.isHeadless()
-
-
方法详细信息
-
addNotify
public void addNotify()
创建按钮的对等体。 按钮的对等体允许应用程序更改按钮的外观,而不会更改其功能。- 重写:
-
addNotify在Component - 另请参见:
-
Component.getToolkit()
-
getLabel
public String getLabel()
获取此按钮的标签。- 结果
-
按钮的标签,或
null如果按钮没有标签。 - 另请参见:
-
setLabel(java.lang.String)
-
setLabel
public void setLabel(String label)
将按钮的标签设置为指定的字符串。- 参数
-
label- 新标签,或null如果按钮没有标签。 - 另请参见:
-
getLabel()
-
setActionCommand
public void setActionCommand(String command)
设置此按钮触发的操作事件的命令名称。 默认情况下,此操作命令设置为与按钮的标签相匹配。- 参数
-
command- 用于设置按钮的动作命令的字符串。 如果字符串为null则将该命令设置为与按钮的标签相匹配。 - 从以下版本开始:
- 1.1
- 另请参见:
-
ActionEvent
-
getActionCommand
public String getActionCommand()
返回此按钮触发的操作事件的命令名称。 如果命令名为null(默认),则此方法返回按钮的标签。- 结果
- 该按钮的动作命令名称(或标签)
-
addActionListener
public void addActionListener(ActionListener l)
添加指定的动作侦听器以从此按钮接收动作事件。 当用户按下或释放鼠标在此按钮上时,会发生动作事件。 如果l为空,则不会抛出任何异常,也不会执行任何操作。有关AWT线程模型的详细信息,请参阅AWT Threading Issues 。
- 参数
-
l- 动作侦听器 - 从以下版本开始:
- 1.1
- 另请参见:
-
removeActionListener(java.awt.event.ActionListener),getActionListeners(),ActionListener
-
removeActionListener
public void removeActionListener(ActionListener l)
删除指定的动作侦听器,使其不再从此按钮接收到动作事件。 当用户按下或释放鼠标在此按钮上时,会发生动作事件。 如果l为空,则不会抛出任何异常,也不会执行任何操作。有关AWT线程模型的详细信息,请参阅AWT Threading Issues 。
- 参数
-
l- 动作侦听器 - 从以下版本开始:
- 1.1
- 另请参见:
-
addActionListener(java.awt.event.ActionListener),getActionListeners(),ActionListener
-
getActionListeners
public ActionListener[] getActionListeners()
返回在此按钮上注册的所有动作侦听器的数组。- 结果
-
所有这个按钮的
ActionListeners或一个空数组,如果没有动作侦听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addActionListener(java.awt.event.ActionListener),removeActionListener(java.awt.event.ActionListener),ActionListener
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回当前注册为FooListener的所有对象的数组Button。FooListener使用addFooListener注册。您可以使用类文字指定
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
- 另请参见:
-
getActionListeners()
-
processEvent
protected void processEvent(AWTEvent e)
处理此按钮上的事件。 如果事件是一个ActionEvent的实例,则此方法将调用processActionEvent方法。 否则,它将在超类上调用processEvent。请注意,如果事件参数为
null该行为未指定,并可能导致异常。- 重写:
-
processEvent在Component - 参数
-
e- 事件 - 从以下版本开始:
- 1.1
- 另请参见:
-
ActionEvent,processActionEvent(java.awt.event.ActionEvent)
-
processActionEvent
protected void processActionEvent(ActionEvent e)
通过将此按钮发送到任何已注册的ActionListener对象来处理此按钮上发生的操作事件。除非为此按钮启用了操作事件,否则不会调用此方法。 当发生以下情况之一时,启用操作事件:
- 一个
ActionListener对象通过addActionListener注册。 - 通过
enableEvents启用操作事件。
请注意,如果事件参数为
null则行为未指定,可能会导致异常。- 参数
-
e- 动作事件 - 从以下版本开始:
- 1.1
- 另请参见:
-
ActionListener,addActionListener(java.awt.event.ActionListener),Component.enableEvents(long)
- 一个
-
paramString
protected String paramString()
返回表示此Button的状态的字符串。 该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null。- 重写:
-
paramString在Component - 结果
- 该按钮的参数字符串
-
getAccessibleContext
@BeanProperty(expert=true, description="The AccessibleContext associated with this Button.") public AccessibleContext getAccessibleContext()
获取AccessibleContext与此Button相关联。 对于按钮,AccessibleContext需要一个的形式AccessibleAWTButton。 如有必要,将创建一个新的AccessibleAWTButton实例。- Specified by:
-
getAccessibleContext在接口Accessible - 重写:
-
getAccessibleContext在Component - 结果
-
一个
AccessibleAWTButton,作为这个AccessibleContext的Button - 从以下版本开始:
- 1.3
-
-