【发布时间】:2015-06-03 02:37:05
【问题描述】:
我有一个JMenuItem,我想接收用户输入。用户必须能够通过鼠标或键盘启动项目的功能。
该项目的功能包含要打开的JDialog。此对话框侦听释放的 ENTER 键并在释放 ENTER 时启动它自己的功能。
当用户使用 ENTER(按下键)点击JMenuItem 时,他将打开对话框。当他松开 ENTER 时,他将 - 这就是问题 - 导致对话框的功能启动。 (当用户放开 ENTER 键时,他将向现在打开的 JDialog 触发一个事件。我不想要那个。我想要两个单独的步骤:第一个:使用 ENTER 选择 JMenuItem(完成笔画或 ENTER 释放),第 2 次:使用 ENTER 在 JDialog 中启动新功能(完整笔画或 ENTER 释放)。
到目前为止,我已经尝试了几件事,但我无法让JMenuItem 接收密钥释放事件。这似乎是我目前无法解决的一些焦点问题。
如何解决这个问题?
如果解释有点混乱,可以这样总结:
我希望
JMenuItem接收 ENTER-key-released-events 并对其做出反应,我不希望它对 ENTER-key-pressed-events 做出反应。
一个小代码示例可能会显示我的问题:
编辑
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
public class MenuItemProblem {
public static void openDialog(JFrame frame){
Action enterReleasedAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
// This action shall be triggered by releasing
// the enter key in the JDialog.
// (There are two times events of the released enter key shall be
// evaluated during the application runs, of which this is the second one.)
// But: It is triggered by the first release of enter in the application,
// which is the one to choose the menu item.
System.out.println("Dialog: Enter was released!");
}
};
JDialog dialog = new JDialog(frame, "My dialog", true);
JRootPane rootPane = dialog.getRootPane();
ActionMap actionMap = rootPane.getActionMap();
String enterReleased = "my_enter_released_function";
actionMap.put(enterReleased, enterReleasedAction);
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
// Keystroke for releasing the enter key
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), enterReleased);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setMinimumSize(new Dimension(260, 300));
dialog.setResizable(true);
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new JFrame("My App");
JMenuItem item = new JMenuItem("My item");
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// By releasing enter (and mouse clicks, space and so on)
// this action shall be triggered.
// It shall not be triggered by enter key pressed,
// for this would cause the action of the called dialog
// do be triggered.
// But: It is triggered by enter pressed!
// (There are two times events of the released enter key shall be
// evaluated during the application runs, of which this is the first one.)
System.out.println("Choosing the menu item");
openDialog(frame);
}
});
JMenu menu = new JMenu("My menu");
menu.add(item);
JMenuBar bar = new JMenuBar();
bar.add(menu);
// Register alt key to be caught
Action altAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
MenuElement[] elements = MenuSelectionManager.defaultManager().getSelectedPath();
if(elements.length > 0){
MenuSelectionManager.defaultManager().clearSelectedPath();
} else{
menu.doClick();
}
}
};
JRootPane rootPane = frame.getRootPane();
ActionMap actionMap = rootPane.getActionMap();
String altActionKey = "alt_key_for_menu";
actionMap.put(altActionKey, altAction);
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true);
inputMap.put(ks, altActionKey);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(bar);
frame.setSize(350, 250);
frame.setVisible(true);
}
}
是的,如果你们能指出我正确的方向,我会很高兴。 提前感谢并感谢您的所有努力!
【问题讨论】:
-
默认情况下不就是这样吗?
-
对于 JMenuItem(对于 JMenu 是否有另一个侦听器)默认实现正确(如 MadProgrammer 所述),您可以使用 MenuListener、MenuKeyListener 和 ButtonModel 中的事件,但那里缺少.. . 看看助记符和加速器,
-
该项目的功能包含要打开的 JDialog。此对话框侦听释放的回车键。 == 我希望只有一个带有 CardLayout 的 JDialog
-
@MadProgrammer:不,该项目接收到按下事件而不是释放事件。
-
没意义,用键盘打开一个
JMenu,选择一个JMenuItem并按下Enter会触发JMenuItem的关联ActionListener,所有这些额外的@987654334 @ stuff 只是增加了不必要的复杂性
标签: java swing keyboard-events jmenuitem keyrelease