java 不使用 alt 键的 jButton 快捷键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4240740/
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
Shortcut key for jButton without using alt key
提问by Rich C
In SWT you can give any button a shortcut key simply by adding &
in front of the letter in the button label. For example, if my button label is &Play
, I can activate the button by hitting letter p
on the keyboard.
在 SWT 中,您只需&
在按钮标签中的字母前添加即可为任何按钮提供快捷键。例如,如果我的按钮标签是&Play
,我可以通过敲击p
键盘上的字母来激活按钮。
In Swing, you can add a shortcut key using the mnemonic
property. However, you need to hit alt+p
to activate the button. This is really most appropriate for menu shortcuts. I want to activate the button with a letter press and no alt modifier.
在 Swing 中,您可以使用该mnemonic
属性添加快捷键。但是,您需要点击alt+p
以激活按钮。这真的最适合菜单快捷方式。我想用字母按下而没有 alt 修饰符来激活按钮。
I've seen this post on how to do it, but it seems absurdly complicated. Is there an easier way to do this?
我已经看过这篇关于如何做到这一点的帖子,但它似乎非常复杂。有没有更简单的方法来做到这一点?
http://linuxjavaprogrammer.blogspot.com/2008/01/java-swing-jbutton-keyboard-shortcuts.html
http://linuxjavaprogrammer.blogspot.com/2008/01/java-swing-jbutton-keyboard-shortcuts.html
Update:After @camickr suggestion, I ended up using this code. I couldn't find any clear and simple example online, so hopefully this will help people out.
更新:在@camickr 建议之后,我最终使用了此代码。我在网上找不到任何清晰简单的例子,所以希望这能帮助人们。
// play is a jButton but can be any component in the window
play.getInputMap(play.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_P, 0), "play");
play.getActionMap().put("play", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
playActionPerformed(e); // some function
}
});
采纳答案by camickr
Yes, Swing was designed to use Key Bindings. So instead of adding an ActionListener to the button you add an Action. Then that Action can be shared by the button or a menu item. You can also assign any number of KeyStrokes to invoke the Action by using the KeyBindings. The tutorial also has a section on Actions which explains why using an Action is beneficial.
是的,Swing 旨在使用Key Bindings。因此,不是向按钮添加 ActionListener,而是添加 Action。然后可以通过按钮或菜单项共享该操作。您还可以使用 KeyBindings 分配任意数量的 KeyStrokes 来调用 Action。本教程还有一个关于 Actions 的部分,它解释了为什么使用 Action 是有益的。
JComponent has a registerKeyboardAction(...) method which basically does the InputMap/ActionMap bindings for you, but it also has to wrap the ActionListener in a wrapper Action so its preferrable for you to do you own bindings.
JComponent 有一个 registerKeyboardAction(...) 方法,它基本上为您完成 InputMap/ActionMap 绑定,但它还必须将 ActionListener 包装在包装器 Action 中,因此您最好自己进行绑定。
回答by Andy Balaam
Further to camickr's answer, I am now using a little utility function like this:
除了 camickr 的回答之外,我现在正在使用一个像这样的小实用函数:
public static void clickOnKey(
final AbstractButton button, String actionName, int key )
{
button.getInputMap( JButton.WHEN_IN_FOCUSED_WINDOW )
.put( KeyStroke.getKeyStroke( key, 0 ), actionName );
button.getActionMap().put( actionName, new AbstractAction()
{
@Override
public void actionPerformed( ActionEvent e )
{
button.doClick();
}
} );
}
Now to set the keyboard shortcut for a button I just do:
现在要为一个按钮设置键盘快捷键,我只是这样做:
clickOnKey( playButton, "play", KeyEvent.VK_P );
回答by Brian Knoblauch
I had a similar problem with a dynamically constructed (based on data input) form and just ended up attaching a keyListener action to the buttons. On form construction I parse the Component tree for the buttons and attach the listener. The listener than also parses the tree and matches the keypress with the appropriate button (via the text in the button), since I have no idea which one will have focus at any given time, and fires the button doClick... It's ugly, feels hackish, and has got to be a bit processor intensive, but it allows the flexibility I need for this particular dynamic form...
我在动态构造(基于数据输入)表单时遇到了类似的问题,并且最终将 keyListener 操作附加到按钮。在表单构建中,我解析按钮的组件树并附加侦听器。侦听器还解析树并将按键与适当的按钮(通过按钮中的文本)相匹配,因为我不知道在任何给定时间哪个将具有焦点,并触发按钮 doClick ......这很难看,感觉很hackish,并且必须有点处理器密集型,但它允许我为这种特定的动态形式提供灵活性......