如何将 Enter 指定为 Java 应用程序中所有 JButton 的触发键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/440209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I assign Enter as the trigger key of all JButtons in my Java application?
提问by Alfred B. Thordarson
I'm writing a Java Swing application using the Metal look-and-feel. Every time there is a JButton in my application the user uses the Tab key to move the focus to the button and then hits the Enter key. Nothing happens! If he hits the Space key the button events are fired. How do I assign the Enter key to trigger the same events as the Space key? Thank you for your help.
我正在使用 Metal 外观编写 Java Swing 应用程序。每次我的应用程序中有 JButton 时,用户都使用 Tab 键将焦点移动到按钮上,然后按下 Enter 键。没发生什么事!如果他按下 Space 键,按钮事件就会被触发。如何分配 Enter 键以触发与 Space 键相同的事件?感谢您的帮助。
采纳答案by Alfred B. Thordarson
I found the following:
我发现了以下内容:
http://tips4java.wordpress.com/2008/10/25/enter-key-and-button/
http://tips4java.wordpress.com/2008/10/25/enter-key-and-button/
Where Rob Camick writes that when using JDK5 and later you simply add...
Rob Camick 写道,在使用 JDK5 及更高版本时,您只需添加...
UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
...to the application to solve the problem. This did the trick for me! And I can't imagine anything simpler. However, when using older versions of Java you will have to do something like Richard and Peter describe in their answers to this question.
...应用程序来解决问题。这对我有用!我无法想象任何更简单的事情。但是,在使用旧版本的 Java 时,您必须执行 Richard 和 Peter 在他们对这个问题的回答中描述的操作。
回答by Richard Campbell
You do it by assigning an input / action map for the Enter key. Something like the following:
您可以通过为 Enter 键分配输入/操作映射来实现。类似于以下内容:
// save the command mapping for space
Object spaceMap = button.getInputMap.get(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true));
// add a mapping from enter to the same command.
button.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),spaceMap);
回答by davetron5000
You can also set the "default button" to the button most recently focussed.
您还可以将“默认按钮”设置为最近聚焦的按钮。
I did this on an application, and all methods of doing this are a nightmare to maintain and debug. The fact is, this is clearly not what the designers of Swing intended to happen.
我在一个应用程序上这样做了,所有这样做的方法都是维护和调试的噩梦。事实是,这显然不是 Swing 的设计者想要发生的。
回答by Peter ?tibrany
Here is complete example. Richard was close, but you also need to map pressed ENTER to action, not just released. To make it work for ALL buttons, I have put this mapping to default input map for buttons. Add imports, and it should be runnable.
这是完整的示例。理查德很接近,但您还需要将按下的 ENTER 映射到操作,而不仅仅是释放。为了使其适用于所有按钮,我已将此映射置于按钮的默认输入映射中。添加导入,它应该是可运行的。
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
@Override
public void run() {
setupEnterActionForAllButtons();
JFrame frame = new JFrame("Button test");
frame.getContentPane().add(createButton(), BorderLayout.NORTH);
frame.getContentPane().add(createButton(), BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private void setupEnterActionForAllButtons() {
InputMap im = (InputMap) UIManager.getDefaults().get("Button.focusInputMap");
Object pressedAction = im.get(KeyStroke.getKeyStroke("pressed SPACE"));
Object releasedAction = im.get(KeyStroke.getKeyStroke("released SPACE"));
im.put(KeyStroke.getKeyStroke("pressed ENTER"), pressedAction);
im.put(KeyStroke.getKeyStroke("released ENTER"), releasedAction);
}
private JButton createButton() {
JButton b = new JButton("press enter");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Pressed");
}
});
return b;
}
}
回答by Draemon
Actually, this is a look and feel issue. It is (and should be) up to the look and feel as to which key triggers the focused button.
实际上,这是一个外观和感觉问题。它(并且应该)取决于哪个键触发聚焦按钮的外观。
The "default button" work-around works since the L&F you're using uses enter for the default button.
“默认按钮”解决方法有效,因为您使用的 L&F 使用 enter 作为默认按钮。
Peter's workaround explicitly changes the L&F default "focus action" key - which is somewhat more convincing if you ask me.
Peter 的解决方法明确更改了 L&F 默认的“焦点操作”键——如果你问我的话,这更有说服力。
I would add that I don't think many users would want to tab to the button then hit enter (most won't even notice the focus indicator) - they want the default action to be the "right" one and work wherever they press enter. This can only be done with input maps as Richard suggests.
我想补充一点,我不认为很多用户会想要点击按钮然后按 Enter(大多数人甚至不会注意到焦点指示器)-他们希望默认操作是“正确”的操作,并且无论他们按下什么位置都可以工作进入。正如理查德建议的那样,这只能通过输入映射来完成。
I would certainly suggest getting a very clear picture of what your users actually want and expect (preferably with reference to other apps they use) before changing anything globally.
我当然建议在全局更改任何内容之前,非常清楚地了解您的用户实际想要和期望的内容(最好参考他们使用的其他应用程序)。
回答by rinjan
Extension to above answers to do same with radio , checkboxes. Called this before creating components
对上述答案的扩展与收音机、复选框相同。在创建组件之前调用它
void setupEnterAction(String componentName){
String keyName = componentName + ".focusInputMap";
InputMap im = (InputMap) UIManager.getDefaults().get(keyName);
Object pressedAction = im.get(KeyStroke.getKeyStroke("pressed SPACE"));
Object releasedAction = im.get(KeyStroke.getKeyStroke("released SPACE"));
im.put(KeyStroke.getKeyStroke("pressed ENTER"), pressedAction);
im.put(KeyStroke.getKeyStroke("released ENTER"), releasedAction);
}
public void setEnterEvent(){
setupEnterAction("Button");
setupEnterAction("RadioButton");
setupEnterAction("CheckBox");
}