有人能帮我理解一下在Java中ACCELERATOR_KEY是如何工作的吗?

huangapple go评论66阅读模式
英文:

Could someone help me understand how the ACCELERATOR_KEY works in Java?

问题

我正在尝试理解 ACCELERATOR_KEY 的用法及其作用。我在网上找到了一个代码示例,展示了它的用法,但当我运行代码时似乎没有发生任何事情。

如果我必须猜测的话,ACCELERATOR_KEY 似乎允许用户为某个操作分配一个键盘快捷键,但在这个示例中,当我按下 'A' 键时,没有任何反应。是否有任何想法或解释将不胜感激!谢谢!

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class Main {
  public static void main(String[] a) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Action action = new ShowAction();
    JCheckBox button = new JCheckBox(action);

    frame.add(button, BorderLayout.CENTER);
    frame.setSize(350, 150);
    frame.setVisible(true);
  }
}

class ShowAction extends AbstractAction {
  public ShowAction() {
    super("About");
  
    putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("A"));
    putValue(Action.NAME, "Go to number ");
  }
  
  public void actionPerformed(ActionEvent actionEvent) {
    System.out.println("About Swing");
  }
}
英文:

I am trying to understand how the ACCELERATOR_KEY is used and what it does. I found a code example online that shows how it is used, but when I run the code nothing seems to happen.

If I had to guess it seems the ACCELERATOR_KEY allows the user to assign a keyboard command to something, but in this example when I press ‘A’, nothing happens. Any ideas or explainations would be most appreciated! Thank you!

    // w w w  .  java 2  s .c o  m

   import java.awt.BorderLayout;
   import java.awt.event.ActionEvent;
   import javax.swing.AbstractAction;
   import javax.swing.Action;
   import javax.swing.JCheckBox;
   import javax.swing.JFrame;
   import javax.swing.KeyStroke;





     public class Main {
      public static void main(String[] a) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Action action = new ShowAction();
        JCheckBox button = new JCheckBox(action);

        frame.add(button, BorderLayout.CENTER);
        frame.setSize(350, 150);
        frame.setVisible(true);
      }
    }

     class ShowAction extends AbstractAction {
       public ShowAction() {
         super("About");

         putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("A"));
         putValue(Action.NAME, "Go to number ");

      }
       public void actionPerformed(ActionEvent actionEvent) {
         System.out.println("About Swing");
      }
    }

答案1

得分: 1

> 如果我必须猜的话,ACCELERATOR_KEY 似乎允许用户为某个操作分配一个键盘命令。

正确。

然而,如果您阅读 Action API,您会注意到 ACCELERATOR_KEY 仅用于扩展了 JMenuItem(除了 JMenu 之外)的组件。

如果您想要使用“A”作为 KeyStroke 来调用 JCheckBoxAction,那么您需要使用 Key Bindings 手动进行绑定,方法是使用复选框的 InputMapActionMap

请阅读 Swing 教程中关于如何使用键绑定的部分,以获取更多信息。

请注意,教程还有一个关于“如何使用菜单”的部分,该部分的演示代码演示了如何使用加速键。

您还可以尝试“如何使用操作”部分。该演示中使用的操作被菜单项和按钮共同使用。您可以尝试为操作添加加速键,以查看这两个组件之间的区别。

英文:

> If I had to guess it seems the ACCELERATOR_KEY allows the user to assign a keyboard command to something

Correct.

However, if you read the ActionAPI you will see that the ACCELERATOR_KEY is only used for components that extend JMenuItem (except for JMenu).

If you want to use "A" as a KeyStroke to invoke an Action for the JCheckBox, then you need to use Key Bindings to manually do the binding by using the InputMap and ActionMap of the check box.

Read the section from the Swing turtorial on How to Use Key Bindings for more information.

Note the tutorial also has a section on How to Use Menus and the demo code in that section demonstrates how to use an accelerator.

You can also try the How to Use Actions section. The Actions used in that demo are used by both a menu item and a button. You can try adding an accelerator the the Action to see the difference between the two components.

huangapple
  • 本文由 发表于 2020年9月6日 02:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63757206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定