简体   繁体   English

设置键绑定以执行与我的动作侦听器中相同的操作

[英]Setting a key-binding to perform the same action as in my action listener

I have a JButton that's attached to an ActionListener, but I also wanted to add a shortcut key to the button to be more user-friendly. 我有一个附加到ActionListener的JButton,但我还想为按钮添加一个快捷键以使用户更友好。 Say, the user can click the button and the program performs some function "f" or the user can also press "Enter" on the keyboard to perform the same function f. 比如,用户可以单击按钮,程序执行某些功能“f”,或者用户也可以按键盘上的“Enter”执行相同的功能f。 So here's what the gist of my code looks like 所以这就是我的代码的主旨

private JButton button;

public static void main(String[] args){
    Action buttonListener = new AbstractAction() {
         public void actionPerformed(ActionEvent e) {
                //Perform function f    
         }
    };

button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"),
                        "test");
button.getActionMap().put("test",
                         buttonListener);

button.addActionListener(new OtherListener());
}

private class OtherListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //Perform function f
    }
}

Seems a bit tedious having to add an Action and an ActionListener to do the same thing. 看起来有点乏味,不得不添加Action和ActionListener来做同样的事情。 Maybe I'm not seeing it, but is there a way to cut the code down so I can eliminate the Action and just use the actionListener? 也许我没有看到它,但有没有办法减少代码,所以我可以消除Action并只使用actionListener? I was thinking switching the buttonListener parameter in the getActionMap().put() method to but the method only takes Action types. 我在考虑在getActionMap()。put()方法中切换buttonListener参数,但该方法只接受Action类型。

Action extends ActionListener , so you should be able to define a single Action and use it wherever you need an ActionListener . Action扩展了ActionListener ,因此您应该能够定义单个Action并在需要ActionListener任何地方使用它。

eg 例如

public static void main(String[] args){
    Action buttonListener = new Action() {
         public void actionPerformed(ActionEvent e) {
                //Perform function f    
         }
    };
    button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
        .put(KeyStroke.getKeyStroke("ENTER"), "test");
    button.getActionMap().put("test", buttonListener);
    button.addActionListener(buttonListener);
}

JRootPane has a method setDefaultButton(...) that will do what you want. JRootPane有一个方法setDefaultButton(...)可以做你想要的。 You will need to get the root pane from the top-level container, then you can call this method passing a reference to your JButton, and it will perform its action when enter is pressed on the GUI. 您需要从顶级容器中获取根窗格,然后您可以调用此方法传递对JButton的引用,并且当在GUI上按下enter时它将执行其操作。 And this makes sense when you think about it as "enter" is a special key, one whose behavior should be the responsibility of the GUI, not a single button. 当你想到它时,这是有道理的,因为“输入”是一个特殊的键,其行为应该是GUI的责任,而不是单个按钮。

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

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