如图4-1所示,AbstractButton类是作用在幕后作为所用 的Swing按钮组件的一个重要Swing类。在本章稍后的JButton类中所描述的JButton是最简单的子类。其余的子类将会在后续的章节中进行描述。
所有的AbstractButton子类使用ButtonModel接口来存储数据模型。DefaultButtonModel类是所使用的默认实现。另外,我们可以将任意的AbstractButton对象组合为一个ButtonGroup。尽管这种组合对于JRadioButton与JRadioButtonMenuItem组件最为自然,然而任意的AbstractButton子类都会起作用。
表4-11列出了AbstractButton子类所共享的32个属性。这些属性可以使得我们自定义所有按钮的外观。
属性名 | 数据类型 | 访问性 |
action | Action | 读写绑定 |
actionCommand | String | 读写 |
actionListeners | ActionListener[] | 只读 |
borderPainted | boolean | 读写绑定 |
changeListeners | ChangeListener[] | 只读 |
contentAreaFilled | boolean | 读写绑定 |
disabledIcon | Icon | 读写绑定 |
disabledSelectedIcon | Icon | 读写绑定 |
disabledMnemonicIndex | int | 读写绑定 |
enabled | boolean | 只写 |
focusPainted | boolean | 读写绑定 |
horizontalAlignment | int | 读写绑定 |
horizontalTextPosition | int | 读写绑定 |
icon | Icon | 读写绑定 |
iconTextGap | int | 读写绑定 |
itemListeners | ItemListener[] | 只读 |
layout | LayoutManager | 只写 |
margin | Insets | 读写绑定 |
mnemonic | char | 读写绑定 |
mnemonic | int | 只写 |
model | ButtonModel | 读写绑定 |
multiClickThreshhold | long | 读写 |
pressedIcon | Icon | 读写绑定 |
rolloverEnabled | boolean | 读写绑定 |
rolloverIcon | Icon | 读写绑定 |
rolloverSelectedIcon | Icon | 读写绑定 |
selected | boolean | 读写 |
selectedIcon | Icon | 读写绑定 |
selectedObjects | Object[] | 只读 |
text | String | 读写绑定 |
UI | ButtonUI | 读写 |
verticalAlignment | int | 读写绑定 |
verticalTextPosition | int | 读写绑定 |
在这里值得一提的就是multiClickThreshhold。这个属性表示以毫秒计数的时间。如果一个按钮在这段时间间隔被鼠标多次选中,并不会产生额外的动作事件。默认情况下这个属性值为0,意味着每一次点击都会产生一个事件。为了避免在重要的对话框中偶然重复提交动作的发生,应将这个属性值设置0以上的合理值。
ButtonModel/Class DefaultButtonModel接口
ButtonModel接口被用来描述AbstractButton组件的当前状态。另外,他描述了为所有不同的AbstractButton子类所支持的事件监听器对象的集合。其定义如下:
public interface ButtonModel extends ItemSelectable { // Properties public String getActionCommand(); public void setActionCommand(String newValue); public boolean isArmed(); public void setArmed(boolean newValue); public boolean isEnabled(); public void setEnabled(boolean newValue); public void setGroup(ButtonGroup newValue); public int getMnemonic(); public void setMnemonic(int newValue); public boolean isPressed(); public void setPressed(boolean newValue); public boolean isRollover(); public void setRollover(boolean newValue); public boolean isSelected(); public void setSelected(boolean newValue); // Listeners public void addActionListener(ActionListener listener);