简体   繁体   English

Java Swing L&F 原生键盘快捷键

[英]Java Swing L&F native keyboard shortcuts

How do I leverage standard operating system keyboard shortcuts in a Java Swing application?如何在 Java Swing 应用程序中利用标准操作系统键盘快捷键? I see how to add a javax.swing.JToolbar or a menu bar to my application, but it doesn't appear to be bound to de facto standard keyboard shortcuts (like Ctrl+S for Save).我看到了如何将javax.swing.JToolbar或菜单栏添加到我的应用程序,但它似乎并未绑定到事实上的标准键盘快捷键(如用于保存的 Ctrl+S)。

Building off of other people's answers, I thought I'd share what I did in my own app...根据其他人的答案,我想我会分享我在自己的应用程序中所做的事情......

First, I have a class variable which is declared and initialized like this:首先,我有一个 class 变量,它的声明和初始化如下:

private static int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

Next, in the same class, I have this method:接下来,在同一个class中,我有这个方法:

private static void addNewMenuItem(JMenu rootMenu, String itemName,
        int itemMnemonic, ActionListener itemActionListener) {
    JMenuItem menuItem = new JMenuItem(itemName, itemMnemonic);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(itemMnemonic, keyMask)); // <-- Where I use keyMask.
    menuItem.addActionListener(itemActionListener);
    rootMenu.add(menuItem);
}

Now, all that I have to do to create a new menu is write something like:现在,创建新菜单所需要做的就是编写如下内容:

addNewMenuItem(fileMenu, "Save...", KeyEvent.VK_S, saveListener);

As you might imagine, it's a really handy method for cleaning up code where I'm setting up a menu with dozens of items.正如您可能想象的那样,在我设置包含数十个项目的菜单时,这是一种清理代码的非常方便的方法。 I hope that helps someone.我希望这对某人有所帮助。 (I didn't go the Action route because it would require me to set up a new class for each action... that seemed like an irritating limitation to have to get around. I'm pretty sure this code is shorter than that code would be.) (我没有 go 动作路线,因为它需要我为每个动作设置一个新的 class ......这似乎是一个令人恼火的限制,必须绕过。我很确定这个代码比那个代码短将会。)

You can use Toolkit.getDefaultToolkit.getMenuShortcutKeyMask() to get the correct modifier key per-platform ( Ctrl , Command , etc.), but I'm not aware of any way to find out what the "standard" shortcuts for an action are, without defining them yourself.您可以使用Toolkit.getDefaultToolkit.getMenuShortcutKeyMask()获取每个平台的正确修饰键( CtrlCommand等),但我不知道有什么方法可以找出操作的“标准”快捷方式是什么,而无需自己定义它们。

Something that excels a little bit more at being native, like SWT, might be better at this kind of thing, though.不过,像 SWT 这样更擅长本地化的东西可能更擅长这种事情。

Read the Swing tutorial on How to Use Actions .阅读有关如何使用操作的 Swing 教程。

An Action is basically an ActionListener with a few more properties. Action 基本上是一个带有更多属性的 ActionListener。 You can define the text, mnemonic and accelerators for the Action.您可以为操作定义文本、助记符和加速键。 Then you can use the same Action to create a JButton which you add to a toolbar or a JMenuItem which you add to a menu.然后,您可以使用相同的操作来创建添加到工具栏的 JButton 或添加到菜单的 JMenuItem。

You should manually bind your keyboard shortcut to the desired menu entry.您应该手动将键盘快捷键绑定到所需的菜单条目。 For example using the setAccelerator() method on JMenuItem.例如在 JMenuItem 上使用 setAccelerator() 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM