Java 密钥绑定不工作,未执行操作

Java 密钥绑定不工作,未执行操作,java,swing,key,action,Java,Swing,Key,Action,我只是想了解键绑定器是如何工作的,似乎我误解了Java教程中的一些内容。代码如下: public class KeyBinder { public static void main(String[] args) { //making frame and label to update when "g" key is pressed. JLabel keybinderTestLabel; JFrame mainFrame = new

我只是想了解键绑定器是如何工作的,似乎我误解了Java教程中的一些内容。代码如下:

public class KeyBinder {

    public static void main(String[] args) {

        //making frame and label to update when "g" key is pressed.

        JLabel keybinderTestLabel;

        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(300,75);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);

        keybinderTestLabel = new JLabel("Press the 'g' key to test the key binder.");
        mainFrame.add(keybinderTestLabel);

        Action gPressed = new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent e) {
                keybinderTestLabel.setText("Key Binding Successful.");
                System.out.println("Key Binding Successful.");
                //Testing to see if the key binding was successful.
            }

        };


        keybinderTestLabel.getInputMap().put(KeyStroke.getKeyStroke("g"), "gPressed");
        keybinderTestLabel.getActionMap().put("gPressed", gPressed);
        /*
         * from my understanding, these two lines map the KeyStroke event of the g key
         * to the action name "gpressed", then map the action name "gpressed" to the action
         * gpressed.
         * 
         */
    }

}
据我所知,我将g键映射到动作名称“gPressed”,然后将其映射到动作
gPressed
。但由于某些原因,当我运行程序时,按g键不会更新文本标签。这里有什么问题?“g”击键实际上没有映射到键盘上的g键吗?

所以,从JavaDocs

public final InputMap getInputMap()
返回所使用的
InputMap
当组件具有焦点时。这是一种方便的方法
getInputMap(聚焦时)

由于
JLabel
不可聚焦,因此这将永远不起作用,相反,您需要提供不同的聚焦条件,例如

keybinderTestLabel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). //...
而且,这是个人的喜好
KeyStroke.getKeyStroke(“g”)
像这样使用
KeyStroke.getKeyStroke
可能会有问题,因为您提供的
字符串的含义非常精确,我永远记不清它应该如何工作(并且没有过多的文档记录)

如果第一个建议无法解决问题,也可以尝试使用JavaDocs中的
KeyStroke.getKeyStroke(KeyEvent.VK_G,0)

public final InputMap getInputMap()
返回所使用的
InputMap
当组件具有焦点时。这是一种方便的方法
getInputMap(聚焦时)

由于
JLabel
不可聚焦,因此这将永远不起作用,相反,您需要提供不同的聚焦条件,例如

keybinderTestLabel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). //...
而且,这是个人的喜好
KeyStroke.getKeyStroke(“g”)
像这样使用
KeyStroke.getKeyStroke
可能会有问题,因为您提供的
字符串的含义非常精确,我永远记不清它应该如何工作(并且没有过多的文档记录)


如果第一个建议无法解决问题,也可以尝试使用
KeyStroke.getKeyStroke(KeyEvent.VK\u G,0)
代替(1+
KeyStroke.getKeyStroke(“G”)
-不起作用。如果要使用字符串,可以使用:
KeyStroke.getKeyStroke(“G”)
。If将注册用于按键盘上的“g”键的绑定。使用字符串时,基本上是在KeyEvent.VK_???之后获取文本???。因此,您也可以使用
KeyStroke.getKeyStroke(“回车”)
来监听“回车”键。或者更容易理解的是使用一个字符参数:
KeyStroke.getKeyStroke('g')
,啊,谢谢!但是对于getKeyStroke(KeyEvent.VK_G,0),0参数在做什么?@Psear它是修饰符(即shift/alt/control)(1+
KeyStroke)。getKeyStroke(“G”)
-不起作用。如果要使用字符串,可以使用:
KeyStroke.getKeyStroke(“G”)
。If将注册用于按键盘上的“g”键的绑定。使用字符串时,基本上是在KeyEvent.VK_???之后获取文本???。因此,您也可以使用
KeyStroke.getKeyStroke(“回车”)
来监听“回车”键。或者更容易理解的是使用一个字符参数:
KeyStroke.getKeyStroke('g')
,啊,谢谢!但是对于getKeyStroke(KeyEvent.VK_G,0),0参数在做什么?@Psear它是修饰符(即shift/alt/control)