Swing菜单与工具栏(四)(一)

2014-11-24 00:38:55 · 作者: · 浏览: 6

6.1.8 JCheckBoxMenuItem类
Swing的JCheckBoxMenuItem组件的行为类似于我们将一个JCheckBox作为一个JMenuItem放置在菜单上。菜单项的数据模型是ToggleButtonModel,我们在第5章进行了描述。他可以使得菜单项具有选中或是未选中状态,同时显示合适的状态图标。因为数据模型是ToggleButtonModel,当JCheckBoxMenuItem位于一个ButtonGroup中时,该组中只有一个JCheckBoxMenuItem可以被选中。然而,这并不是JCheckBoxMenuItem的通常使用方法,并且很可能会迷惑用户。如果我们需要这种行为,我们可以使用JRadioButtonMenuItem,如本章稍后所述。

创建JCheckBoxMenuItem组件

JCheckBoxMenuItem有七个构造函数。这些构造函数可以允许我们初始化文本标签,图标以及初始状态。

public JCheckBoxMenuItem()
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem(); public JCheckBoxMenuItem(String text)
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("Boy"); public JCheckBoxMenuItem(Icon icon)
Icon boyIcon = new ImageIcon("boy-r.jpg");
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem(boyIcon); public JCheckBoxMenuItem(String text, Icon icon)
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("Boy", boyIcon); public JCheckBoxMenuItem(String text, boolean state)
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("Girl", true); public JCheckBoxMenuItem(String text, Icon icon, boolean state)
Icon girlIcon = new ImageIcon("girl-r.jpg");
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("Girl", girlIcon, true); public JCheckBoxMenuItem(Action action)
Action action = ...;
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem(action);

与JCheckBox不同,图标是标签的一部分,而并不是一个单独的设备来表明某项是否被选中。如果在其构造函数中并没有传递文本标签或是图标,菜单项标签部分则会被设置其空的默认值。默认情况下,JCheckBoxMenuItem初始时未选中。

JCheckBoxMenuItem属性

JCheckBoxMenuItem的大部分属性都是由JCheckBoxMenuItem的多个超类继承来的。表6-11列出了JCheckBoxMenuItem所列出的四个属性。

JCheckBoxMenuItem属性

属性名
数据类型
访问性

accessibleContext
AccessibleContext
只读

selectedObjects
Object[]
只读

state
boolean
读写

UIClassID
String
只读

处理JCheckBoxMenuItem选中事件

对于JCheckBoxMenuItem而言,我们可以关联多个事件变体:

JMenuItem中的MenuDragMouseListener与MenuKeyListener
AbstractButton中的ActionListener,ChangeListener与ItemListener
JComponent中的AncestorListener与VetoableChangeListener
Container中的ContainerListener与PropertyChangeListener
Component中的ComponentListener,FocusListener,HierarchyBoundsListener,HierarchyListener,InputMenthodListener,KeyListener,MouseListener,MouseMotionListener以及MouseWheelListener
尽管我们可以监听18种没的事件类型,但是最有趣的还是ActionEvent与ItemEvent,如下所述。

使用ActionListener监听JCheckBoxMenuItem事件

将ActionListener关联到JCheckBoxMenuItem可以使得我们确定菜单何时被选中。监听器会被通知选中事件,但是并不会得到新状态的通知。要确定选中状态,我们必须获取事件源模型并查询选中状态,如下面的示例ActionListener源码所示。这个监听器会依据当前的选中状态修改复选框的文本与图标标签。

ActionListener aListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
Icon girlIcon = new ImageIcon("girl-r.jpg");
Icon boyIcon = new ImageIcon("boy-r.jpg");
AbstractButton aButton = (AbstractButton)event.getSource();
boolean selected = aButton.getModel().isSelected();
String newLabel;
Icon newIcon;
if (selected) {
newLabel = "Girl";
newIcon = girlIcon;
} else {
newLabel = "Boy";
newIcon = boyIcon;
}
aButton.setText(newLabel);
aButton.setIcon(newIcon);
}
};

使用ItemListener监听JCheckBoxMenuItem事件

如果我们使用ItemListener监听JCheckBoxMenuItem选中事件,我们并不需要查询事件源以确定选中状态,事件已经带有这些信息了。依据这个状态,我们可以进行正确的响应。使用ItemListener重新创建ActionListener的行为只