- java.lang.Object
-
- javax.swing.DefaultButtonModel
-
- All Implemented Interfaces:
-
ItemSelectable,Serializable,ButtonModel
- 已知直接子类:
-
JToggleButton.ToggleButtonModel
public class DefaultButtonModel extends Object implements ButtonModel, Serializable
默认实现的一个Button组件的数据模型。警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4版本开始,所有JavaBeans的长期存储支持已被添加到
java.beans软件包中。 请参阅XMLEncoder。- 从以下版本开始:
- 1.2
- 另请参见:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 描述 protected StringactionCommand按钮触发的动作命令字符串。static intARMED标识位掩码中的“布防”位,这表示部分承诺选择/触发按钮。protected ChangeEventchangeEvent每个按钮模型实例只需要一个ChangeEvent因为事件的唯一状态是源属性。static intENABLED标识位掩码中的“已启用”位,表示该按钮可由输入设备(如鼠标指针)选择。protected ButtonGroupgroup按钮所属的按钮组。protected EventListenerListlistenerList在这个模型上存储听众。protected intmnemonic按钮的助记符static intPRESSED标识位掩码中的“已按”位,表示该按钮被按下。static intROLLOVER标识位掩码中的“翻转”位,表示鼠标在按钮上方。static intSELECTED标识位掩码中的“选定”位,表示该按钮已被选中。protected intstateMask该位掩码用于存储按钮的状态。
-
构造方法摘要
构造方法 Constructor 描述 DefaultButtonModel()构造一个DefaultButtonModel。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidaddActionListener(ActionListener l)添加一个ActionListener模型。voidaddChangeListener(ChangeListener l)在模型中添加一个ChangeListener。voidaddItemListener(ItemListener l)为模型添加一个ItemListener。protected voidfireActionPerformed(ActionEvent e)通知所有在此事件类型上通知有兴趣的听众。protected voidfireItemStateChanged(ItemEvent e)通知所有在此事件类型上通知有兴趣的听众。protected voidfireStateChanged()通知所有在此事件类型上通知有兴趣的听众。StringgetActionCommand()返回按钮的动作命令字符串。ActionListener[]getActionListeners()返回在此DefaultButtonModel上注册的所有动作侦听器的数组。ChangeListener[]getChangeListeners()返回在此DefaultButtonModel上注册的所有更改监听器的数组。ButtonGroupgetGroup()返回按钮所属的组。ItemListener[]getItemListeners()返回在此DefaultButtonModel上注册的所有项目侦听器的数组。<T extends EventListener>
T[]getListeners(Class<T> listenerType)返回此模型当前注册为FooListener的所有对象的数组。intgetMnemonic()获取按钮的键盘助记符。Object[]getSelectedObjects()被null返回null。booleanisArmed()表示部分承诺触发按钮。booleanisEnabled()指示按钮是否可以由输入设备(如鼠标指针)选择或触发。booleanisPressed()指示按下按钮。booleanisRollover()表示鼠标在按钮上方。booleanisSelected()指示按钮是否已被选中。voidremoveActionListener(ActionListener l)从模型中移除ActionListener。voidremoveChangeListener(ChangeListener l)从模型中移除ChangeListener。voidremoveItemListener(ItemListener l)从模型中移除ItemListener。voidsetActionCommand(String actionCommand)设置当触发按钮时作为ActionEvent一部分发送的动作命令字符串。voidsetArmed(boolean b)将按钮标记为武装或非武装。voidsetEnabled(boolean b)启用或禁用按钮。voidsetGroup(ButtonGroup group)标识按钮所属的组 - 单选按钮所需的组,它们在组内是相互排斥的。voidsetMnemonic(int key)设置按钮的键盘助记符(快捷键或加速键)。voidsetPressed(boolean b)将按钮设置为按下或未按下。voidsetRollover(boolean b)设置或清除按钮的翻转状态voidsetSelected(boolean b)选择或取消选择按钮。
-
-
-
字段详细信息
-
stateMask
protected int stateMask
该位掩码用于存储按钮的状态。
-
actionCommand
protected String actionCommand
按钮触发的动作命令字符串。
-
group
protected ButtonGroup group
按钮所属的按钮组。
-
mnemonic
protected int mnemonic
按钮的助记符
-
changeEvent
protected transient ChangeEvent changeEvent
每个按钮模型实例只需要一个ChangeEvent因为事件的唯一状态是源属性。 生成的事件的来源总是“这”。
-
listenerList
protected EventListenerList listenerList
在这个模型上存储听众。
-
ARMED
public static final int ARMED
标识位掩码中的“布防”位,这表示部分承诺选择/触发按钮。- 另请参见:
- Constant Field Values
-
SELECTED
public static final int SELECTED
标识位掩码中的“选定”位,表示该按钮已被选中。 只需要某些类型的按钮 - 如单选按钮或复选框。- 另请参见:
- Constant Field Values
-
PRESSED
public static final int PRESSED
标识位掩码中的“已按”位,表示该按钮被按下。- 另请参见:
- Constant Field Values
-
ENABLED
public static final int ENABLED
标识位掩码中的“已启用”位,表示该按钮可由输入设备(如鼠标指针)选择。- 另请参见:
- Constant Field Values
-
ROLLOVER
public static final int ROLLOVER
标识位掩码中的“翻转”位,表示鼠标在按钮上方。- 另请参见:
- Constant Field Values
-
-
方法详细信息
-
setActionCommand
public void setActionCommand(String actionCommand)
设置当触发按钮时作为ActionEvent一部分发送的动作命令字符串。- Specified by:
-
setActionCommand在接口ButtonModel - 参数
-
actionCommand- 标识生成的事件的String - 另请参见:
-
ButtonModel.getActionCommand(),ActionEvent.getActionCommand()
-
getActionCommand
public String getActionCommand()
返回按钮的动作命令字符串。- Specified by:
-
getActionCommand接口ButtonModel - 结果
-
标识生成的事件的
String - 另请参见:
-
ButtonModel.setActionCommand(java.lang.String)
-
isArmed
public boolean isArmed()
表示部分承诺触发按钮。- Specified by:
-
isArmed在接口ButtonModel - 结果
-
true如果按钮已布防,并准备好触发 - 另请参见:
-
ButtonModel.setArmed(boolean)
-
isSelected
public boolean isSelected()
指示按钮是否已被选中。 只需要某些类型的按钮,如单选按钮和复选框。- Specified by:
-
isSelected在接口ButtonModel - 结果
-
如果按钮被选中,
true
-
isEnabled
public boolean isEnabled()
指示按钮是否可以由输入设备(如鼠标指针)选择或触发。- Specified by:
-
isEnabled在接口ButtonModel - 结果
-
如果按钮被启用,
true
-
isPressed
public boolean isPressed()
指示按下按钮。- Specified by:
-
isPressed在接口ButtonModel - 结果
-
true如果按下按钮
-
isRollover
public boolean isRollover()
表示鼠标在按钮上方。- Specified by:
-
isRollover在接口ButtonModel - 结果
-
true如果鼠标在按钮上方
-
setArmed
public void setArmed(boolean b)
将按钮标记为武装或非武装。- Specified by:
-
setArmed在接口ButtonModel - 参数
-
b- 是否应该按下按钮
-
setEnabled
public void setEnabled(boolean b)
启用或禁用按钮。- Specified by:
-
setEnabled在接口ButtonModel - 参数
-
b- 是否应启用按钮 - 另请参见:
-
ButtonModel.isEnabled()
-
setSelected
public void setSelected(boolean b)
选择或取消选择按钮。- Specified by:
-
setSelected在接口ButtonModel - 参数
-
b-true选择按钮,false取消选择按钮
-
setPressed
public void setPressed(boolean b)
将按钮设置为按下或未按下。- Specified by:
-
setPressed在接口ButtonModel - 参数
-
b- 是否应该按下按钮 - 另请参见:
-
ButtonModel.isPressed()
-
setRollover
public void setRollover(boolean b)
设置或清除按钮的翻转状态- Specified by:
-
setRollover接口ButtonModel - 参数
-
b- 按钮是否处于翻转状态 - 另请参见:
-
ButtonModel.isRollover()
-
setMnemonic
public void setMnemonic(int key)
设置按钮的键盘助记符(快捷键或加速键)。- Specified by:
-
setMnemonic在接口ButtonModel - 参数
-
key- 指定加速键的int
-
getMnemonic
public int getMnemonic()
获取按钮的键盘助记符。- Specified by:
-
getMnemonic在接口ButtonModel - 结果
- 指定加速键的int
- 另请参见:
-
ButtonModel.setMnemonic(int)
-
addChangeListener
public void addChangeListener(ChangeListener l)
在模型中添加一个ChangeListener。- Specified by:
-
addChangeListener在接口ButtonModel - 参数
-
l- 要添加的侦听器
-
removeChangeListener
public void removeChangeListener(ChangeListener l)
从模型中移除ChangeListener。- Specified by:
-
removeChangeListener在接口ButtonModel - 参数
-
l- 要删除的侦听器
-
getChangeListeners
public ChangeListener[] getChangeListeners()
返回在此DefaultButtonModel上注册的所有更改监听器的数组。- 结果
-
所有这个模型的
ChangeListeners或一个空数组,如果没有更改监听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addChangeListener(javax.swing.event.ChangeListener),removeChangeListener(javax.swing.event.ChangeListener)
-
fireStateChanged
protected void fireStateChanged()
通知所有在此事件类型上通知有兴趣的听众。 事件实例是懒惰地创建的。- 另请参见:
-
EventListenerList
-
addActionListener
public void addActionListener(ActionListener l)
在模型中添加一个ActionListener。- Specified by:
-
addActionListener在接口ButtonModel - 参数
-
l- 要添加的监听器
-
removeActionListener
public void removeActionListener(ActionListener l)
从模型中移除ActionListener。- Specified by:
-
removeActionListener在接口ButtonModel - 参数
-
l- 要删除的侦听器
-
getActionListeners
public ActionListener[] getActionListeners()
返回在此DefaultButtonModel上注册的所有动作侦听器的数组。- 结果
-
所有这个模型的
ActionListeners或一个空数组,如果没有动作侦听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addActionListener(java.awt.event.ActionListener),removeActionListener(java.awt.event.ActionListener)
-
fireActionPerformed
protected void fireActionPerformed(ActionEvent e)
通知所有在此事件类型上通知有兴趣的听众。- 参数
-
e- 传送给听众的ActionEvent - 另请参见:
-
EventListenerList
-
addItemListener
public void addItemListener(ItemListener l)
在模型中添加一个ItemListener。- Specified by:
-
addItemListener在接口ButtonModel - Specified by:
-
addItemListener在接口ItemSelectable - 参数
-
l- 要添加的监听器 - 另请参见:
-
ItemEvent
-
removeItemListener
public void removeItemListener(ItemListener l)
从模型中移除ItemListener。- Specified by:
-
removeItemListener在接口ButtonModel - Specified by:
-
removeItemListener在接口ItemSelectable - 参数
-
l- 要删除的侦听器 - 另请参见:
-
ItemEvent
-
getItemListeners
public ItemListener[] getItemListeners()
返回在该DefaultButtonModel上注册的所有项目侦听器的数组。- 结果
-
所有这个模型的
ItemListeners或一个空数组,如果没有项目监听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addItemListener(java.awt.event.ItemListener),removeItemListener(java.awt.event.ItemListener)
-
fireItemStateChanged
protected void fireItemStateChanged(ItemEvent e)
通知所有在此事件类型上通知有兴趣的听众。- 参数
-
e- 传送给听众的ItemEvent - 另请参见:
-
EventListenerList
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
在此模型上返回当前注册为FooListener的所有对象的数组。FooListener使用addFooListener方法注册。您可以使用类文字指定
listenerType参数,如FooListener.class。 例如,您可以使用以下代码查询DefaultButtonModel实例m其操作侦听器:ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));如果没有这样的侦听器存在,这个方法返回一个空数组。- 参数类型
-
T- 请求的听众的类型 - 参数
-
listenerType- 所请求的听众的类型; 此参数应指定从java.util.EventListener的接口 - 结果
-
在该模型上注册为
FooListener的所有对象的数组,如果没有添加此类侦听器,则为空数组 - 异常
-
ClassCastException- 如果listenerType没有指定实现java.util.EventListener的类或接口 - 从以下版本开始:
- 1.3
- 另请参见:
-
getActionListeners(),getChangeListeners(),getItemListeners()
-
getSelectedObjects
public Object[] getSelectedObjects()
被null返回null。- Specified by:
-
getSelectedObjects在接口ItemSelectable - 结果
-
所选对象的列表,或
null
-
setGroup
public void setGroup(ButtonGroup group)
标识按钮所属的组 - 单选按钮所需的组,它们在组内是相互排斥的。- Specified by:
-
setGroup在接口ButtonModel - 参数
-
group- 该按钮属于ButtonGroup
-
getGroup
public ButtonGroup getGroup()
返回按钮所属的组。 通常用于单选按钮,它们在组内是相互排斥的。- 结果
-
该按钮所属的
ButtonGroup - 从以下版本开始:
- 1.3
-
-