我已經能夠在我的框架和菜單項中顯示一個彈出菜單,一組單選按鈕可以用箭頭鍵導航。當用戶按Enter/Return時,彈出式菜單不再可見,我認爲可以確定選擇哪個按鈕並分配一個動作。如何捕捉用戶在jpopupmenu中選擇jradiobutton?
private static final JPopupMenu popupSyncTIR = new JPopupMenu();
popupSyncTIR.setLabel("Sensor Synced to Clock");
ButtonGroup grpTIR = new ButtonGroup();
JRadioButtonMenuItem rbTIRMenuItem;
rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-A to clock");
rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16));
grpTIR.add(rbTIRMenuItem);
popupSyncTIR.add(rbTIRMenuItem);
rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-B to clock");
rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16));
rbTIRMenuItem.setSelected(true);
grpTIR.add(rbTIRMenuItem);
popupSyncTIR.add(rbTIRMenuItem);
rbTIRMenuItem = new JRadioButtonMenuItem("Sync TIR-C to clock");
rbTIRMenuItem.setFont(new Font("Dialog", Font.BOLD, 16));
grpTIR.add(rbTIRMenuItem);
popupSyncTIR.add(rbTIRMenuItem);
我還實施了鍵映射整個框架是這樣的:
/**
* alt-T period synch TIR sensor w/clock
*/
@SuppressWarnings("serial")
private static void registerSyncTIRAction() {
javax.swing.Action tirSync = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
setTIRSensorSync();
}
};
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_T,
java.awt.event.InputEvent.ALT_DOWN_MASK);
((JComponent) contentPanel).getActionMap().put("TirSync", tirSync);
((JComponent) contentPanel).getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "TirSync");
}
/**
* Show the TIR popup
*/
private static void setTIRSensorSync() {
popupSyncTIR.show(pnlTIR, 10, 10);
}
如何確定之前用戶按下Enter鍵選擇什麼樣的單選按鈕?
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –