当前位置:编程学习 > JAVA >>

Swing菜单与工具栏(四)

 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的行为只需要对前面所列出的源代码进行简单的修改,如下所示:

ItemListener iListener = new ItemListener() {
   public void itemStateChanged(ItemEvent event) {
     Icon girlIcon = new ImageIcon("girl-r.jpg");
     Icon boyIcon = new ImageIcon("boy-r.jpg");
     AbstractButton aButton = (AbstractButton)event.getSource();
     int state = event.getStateChange();
     String newLabel;
     Icon newIcon;
     if (state == ItemEvent.SELECTED) {
       newLabel = "Girl";
       newIcon = girlIcon;
     } else {
       newLabel = "Boy";
       newIcon = boyIcon;
     }
     aButton.setText(newLabel);
     aButton.setIcon(newIcon);
   }
};

自定义JCheckBoxMenuItem观感

图6-3显示了预安装的观感类型集合下JCheckBoxMenuItem的外观。

表6-12列出了JCheckBoxMenuItem的UIResource相关的属性。JCheckBoxMenuItem组件具有19个不同的属性。

JCheckBoxMenuItem UIResource元素

 
属性字符串
 对象类型
 
CheckBoxMenuItem.acceleratorFont
 Font
 
CheckBoxMenuItem.acceleratorForeground
 Color
 
CheckBoxMenuItem.acceleratorSelectionForeground
 Color
 
CheckBoxMenuItem.actionMap
 ActionMap
 
CheckBoxMenuItem.arrowIcon
 Icon
 
CheckBoxMenuItem.background
 Color
 
CheckBoxMenuItem.border
 Border
 
CheckBoxMenuItem.borderPainted
 Boolean
 
CheckBoxMenuItem.checkIcon
 Icon
 
CheckBoxMenuItem.commandSound
 String
 
CheckBoxMenuItem.disabledForeground
 Color
 
CheckBoxMenuItem.font
 Font
 
CheckBoxMenuItem.foreground
 Color
 
CheckBoxMenuItem.gradient
 List
 
CheckBoxMenuItem.margin
 Insets
 
CheckBoxMenuItem.opaue
 Boolean
 
CheckBoxMenuItem.selectionBackground
 Color
 
CheckBoxMenuItem.selectionForeground
 Color
 
CheckBoxMenuItemUI
 String
 

 

与CheckboxMenuItem.checkIcon属性键值相关联的Icon是显示在JCheckBoxMenuItem上的图标。如果我们不喜欢默认图标,我们可以使用下面的代码行进行修改,在这里假定已经定义并创建了新图标:

UIManager.put("CheckBoxMenuItem.checkIcon", someIcon);为了使得新图标可以显示合适的选中图像,Icon实现必须其paintIcon()方法内检测关联的菜单组件状态。第4章所创建的DiamondIcon对于这个图标并不起作用,因为他并不检测其状态组件。相反,状态是在构造是确定的。列表6-4列出了一个可以使用的图标类。

package net.ariel.ch06;   import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Polygon;   import javax.swing.AbstractButton;
import javax.swing.Icon;   public class DiamondAbstractButtonStateIcon implements Icon {   private final int width = 10;
 private final int height = 10;
 private Color color;
 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,