简体   繁体   English

键绑定不起作用,Java SE,Swing

[英]Key bindings don't work, Java SE, Swing

I'm trying to add a shortcut to my JButton. 我正在尝试为我的JButton添加一个快捷方式。 I've read How to Use Key Bindings tutorial and I also have read this page How to use Key Bindings instead of Key Listeners and a loooooooooooooot of other questions about key bindings, but haven't found any answer for me 我已经阅读了如何使用键绑定教程,我也阅读了本页如何使用键绑定而不是键监听器和关于键绑定的其他问题的loooooooooooooot,但是没有找到任何答案给我

What I've tried: 我尝试过的:

public class Example extends JFrame {

    public static void main(String args[]) {
        Example example = new Example();
    }

    Example(){
        Action action = new Action() {
            @Override
            public Object getValue(String key) {
                return null;
            }

            @Override
            public void putValue(String key, Object value) {

            }

            @Override
            public void setEnabled(boolean b) {

            }

            @Override
            public boolean isEnabled() {
                return false;
            }

            @Override
            public void addPropertyChangeListener(PropertyChangeListener listener) {

            }

            @Override
            public void removePropertyChangeListener(PropertyChangeListener listener) {

            }

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Hello World!");
            }
        };

        JButton button = new JButton("Hello World!");
        button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "doSomething");
        button.getActionMap().put("doSomething", action);
        button.addActionListener(action);

        add(button);
        setVisible(true);
        pack();
    }

}

I've also tried to make it like that: getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "doSmth"); 我也尝试过这样: getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "doSmth");

But nothing seems to be working, what am I doing wrong? 但似乎没有什么工作,我做错了什么?

Your Action has a method called isEnabled that you've implemented. 您的Action有一个名为isEnabled的方法,您已经实现了该方法。 The Javadoc on it states: 它上面的Javadoc说:

/**
 * Returns the enabled state of the <code>Action</code>. When enabled,
 * any component associated with this object is active and
 * able to fire this object's <code>actionPerformed</code> method.
 *
 * @return true if this <code>Action</code> is enabled
 */

Since you return a hardcoded false , the Action is never enabled and the actionPerformed method is never called. 由于返回硬编码的false ,因此永远不会启用Action并且永远不会调用actionPerformed方法。 Your problem is not the binding, it's the action itself! 你的问题不是绑定,而是行动本身!

A simple fix is to change isEnabled to return true, or, even simpler yet, use AbstractAction in place of Action , and override only actionPerformed ( AbstractAction is kind of the "I don't care about all this stuff, just give me the simplest thing possible with one method to implement!") 一个简单的解决方法是将isEnabled更改为true,或者更简单,使用AbstractAction代替Action ,并仅覆盖actionPerformedAbstractAction是一种“我不关心所有这些东西,只是给我最简单的用一种方法实现的事情!“)

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

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