I am trying to add a method which take开发者_如何学运维s the menu selection and passes it to a variable. In this case I would like to take "7" and insert that into the year variable if that is what is selected from teh checkboxmenu. So far have search the internet and sun site with no clear example on how to differentiate the menu items. Any suggestions would be appreciated thanks.
cbMenuItem = new JCheckBoxMenuItem("7 year");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);
cbMenuItem = new JCheckBoxMenuItem("15 year");
cbMenuItem.setMnemonic(KeyEvent.VK_S);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);
menuItem.setActionCommand("7");
Then in your ItemListener you can use the getActionCommand(...) method on the selected item.
精彩评论