java 如何将按下“输入”与单击按钮相关联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4703152/
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 to associate pressing "enter" with clicking button?
提问by Connor
In my swing program I have a JTextField and a JButton. I would like for, once the user presses the "enter" key, the actionListener of the JButton runs. How would I do this? Thanks in advance.
在我的 Swing 程序中,我有一个 JTextField 和一个 JButton。我想,一旦用户按下“回车”键,JButton 的 actionListener 就会运行。我该怎么做?提前致谢。
回答by Hovercraft Full Of Eels
JRootPane has a method setDefaultButton(JButton button) that will do what you want. If your app is a JFrame, it implements the RootPaneContainer interface, and you can get the root pane by calling getRootPane() on the JFrame, and then call setDefaultButton on the root pane that was returned. The same technique works for JApplet, JDialog or any other class that implements RootPaneContainer.
JRootPane 有一个方法 setDefaultButton(JButton button) 可以做你想做的。如果您的应用程序是 JFrame,它实现了 RootPaneContainer 接口,您可以通过在 JFrame 上调用 getRootPane() 来获取根窗格,然后在返回的根窗格上调用 setDefaultButton。相同的技术适用于 JApplet、JDialog 或任何其他实现 RootPaneContainer 的类。
回答by antonis_wrx
there is an example here
这里有一个例子
http://www.java2s.com/Code/Java/Swing-JFC/SwingDefaultButton.htm
http://www.java2s.com/Code/Java/Swing-JFC/SwingDefaultButton.htm
this is what you need: rootPane.setDefaultButton(button2);
这就是你需要的: rootPane.setDefaultButton(button2);
回答by chubbsondubs
Get rid of ActionListeners. That's the old style for doing listeners. Graduate to the Action class. The trick is to understand InputMaps and ActionMaps work. This is a unique feature of Swing that is really quite nice:
摆脱 ActionListeners。这是做听众的旧作风。毕业到行动班。诀窍是理解 InputMaps 和 ActionMaps 的工作原理。这是 Swing 的一个独特功能,非常好:
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
Here's how you do it:
这是你如何做到的:
JPanel panel = new JPanel();
panel.setLayout( new TableLayout( ... ) );
Action someAction = new AbstractAction( "GO" ) {
public void actionPerformed() {
}
};
InputMap input = panel.getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
input.put( KeyStroke.getKeyStroke( "enter", "submit" );
panel.getActionMap().put("submit", someAction );
panel.add( button = new JButton( someAction ) );
panel.add( textField = new JTextField( ) );
Using the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT allows the panel to receive keyboard events from any of it's child (i.e. ancestors). So no matter what component has focus as long as it's inside the panel that keystroke will invoke any action registered under "submit" in the ActionMap.
使用 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT 允许面板从它的任何子项(即祖先)接收键盘事件。因此,无论哪个组件具有焦点,只要它在面板内,击键就会调用 ActionMap 中“提交”下注册的任何操作。
This allows you to reuse Actions in menus, buttons, or keystrokes by sharing them.
这允许您通过共享来重用菜单、按钮或击键中的操作。