java.lang.Object --java.awt.Component --java.awt.Container --javax.swing.JComponent --javax.swing.AbstractButton --javax.swing.JMenuItem --javax.swing.JCheckBoxMenuItem
JCheckBoxMenuItem继承JMenuItem类,因此JCheckBoxMenuItem可以使用JMenuItem所提供的方法,而且JCheckBoxMenuItem也具有 AbstractButton的特性,而JChcekBoxMenuItem和JCheckBox的性质几乎是一样,两者间最大的差别在于JCheckBoxMenuItem是专用在 MenuItem上.我们来看看JCheckBoxMenuItem的构造方法:
由上表可以看出JCheckBoxMenuItem的构造方式与JMenuItem的构造方式几乎一模一样,唯一有差异的是JCheckBoxMenuItem的构 造方式中多了设置选择状况的构造方式,设置选择状态就是决定是否要将构造好的JCheckBoxMenuItem组件设置成默认值.我们来看下 面的范例:
import javax.swing.*; import java.awt.event.*; import java.util.*; import java.awt.*; import com.incors.plaf.alloy.*; import com.incors.plaf.alloy.themes.glass.*; public class JCheckBoxMenuItem1 extends JFrame implements ActionListener {// 由于我们要处理ActionEvent事件,因此要实作 // ActionListener界面. JTextArea theArea = null; public JCheckBoxMenuItem1() { super("JCheckBoxMenuItem1"); theArea = new JTextArea(); theArea.setEditable(false); getContentPane().add(new JScrollPane(theArea)); JMenuBar MBar = new JMenuBar(); MBar.setOpaque(true); JMenu mfile = buildFileMenu(); JMenu mstyle = buildStyleMenu();// 调用buildStyleMenu()方法来建立包含JCheckBoxMenuItem的JMenu. MBar.add(mfile); MBar.add(mstyle); setJMenuBar(MBar); }// end of JCheckBoxMenuItem1() public JMenu buildFileMenu() { JMenu thefile = new JMenu("File"); thefile.setMnemonic('F'); JMenuItem newf = new JMenuItem("New", new ImageIcon("icons/new24.gif")); JMenuItem open = new JMenuItem("Open", new ImageIcon("icons/open24.gif")); JMenuItem close = new JMenuItem("Close", new ImageIcon( "icons/close24.gif")); JMenuItem quit = new JMenuItem("Exit", new ImageIcon("icons/exit24.gif")); newf.setMnemonic('N'); open.setMnemonic('O'); close.setMnemonic('L'); quit.setMnemonic('X'); newf.setAccelerator(KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false)); open.setAccelerator(KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false)); close.setAccelerator(KeyStroke.getKeyStroke('L', java.awt.Event.CTRL_MASK, false)); quit.setAccelerator(KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false)); // 将File Menu内的三个JMenuItem对象加入事件处理模式. newf.addActionListener(this); open.addActionListener(this); close.addActionListener(this); quit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); thefile.add(newf); thefile.add(open); thefile.add(close); thefile.addSeparator(); thefile.add(quit); return thefile; }// end of buildFileMenu() public JMenu buildStyleMenu() { // 建立新的JMenu组件 JMenu style = new JMenu("Style"); // 设置其快捷键 style.setMnemonic('S'); JCheckBoxMenuItem Left = new JCheckBoxMenuItem("Left", new ImageIcon( "icons/left24.gif")); JCheckBoxMenuItem Center = new JCheckBoxMenuItem("Center", new ImageIcon("icons/center24.gif")); JCheckBoxMenuItem Right = new JCheckBoxMenuItem("Right", new ImageIcon( "icons/right24.gif")); JCheckBoxMenuItem Justify = new JCheckBoxMenuItem("Justify", new ImageIcon("icons/justify24.gif")); Left.setMnemonic('L'); Center.setMnemonic('E'); Right.setMnemonic('R'); Justify.setMnemonic('J'); Left.setAccelerator(KeyStroke.getKeyStroke('L', java.awt.Event.SHIFT_MASK, false)); Center.setAccelerator(KeyStroke.getKeyStroke('E', java.awt.Event.SHIFT_MASK, false)); Right.setAccelerator(KeyStroke.getKeyStroke('R', java.awt.Event.SHIFT_MASK, false)); Justify.setAccelerator(KeyStroke.getKeyStroke('J', java.awt.Event.SHIFT_MASK, false)); // 分别将四个JCheckBoxMenuItem加入事件处理模式. Left.addActionListener(this); Center.addActionListener(this); Right.addActionListener(this); Justify.addActionListener(this); style.add(Left); style.add(Center); style.add(Right); style.add(Justify); return style; }// end of buildStyleMenu() public void actionPerformed(ActionEvent ae) { // 为事件处理区段,在这里是将被点选的JMenuItem组件名称填入JTextArea中. try { theArea.append("* action '" + ae.getActionCommand() + "' performed. *\n"); } catch (Exception e) { System.out.println("actionPerformed Exception:" + e); } } public static void main(String[] args) { SwingUtil.setLookAndFeel(); JFrame F = new JCheckBoxMenuItem1(); F.setSize(400, 200); F.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });// end of addWindowListener F.setVisible(true); } // end of main }// end of class JCheckBoxMenuItem1 class SwingUtil { public static final void setLookAndFeel() { try { Font font = new Font("JFrame", Font.PLAIN, 12); Enumeration keys = UIManager.getLookAndFeelDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (UIManager.get(key) instanceof Font) { UIManager.put(key, font); } } AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration", "true"); AlloyTheme theme = new GlassTheme(); LookAndFeel alloyLnF = new AlloyLookAndFeel(theme); UIManager.setLookAndFeel(alloyLnF); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } } }