键绑定未调用AbstractAction

时间:2018-08-12 17:01:49

标签: java key-bindings

我正在制作一个游戏,用户必须按下按键才能四处移动。我正在使用键盘绑定,但是它们不起作用。这些键绑定应该调用Wp类并显示“按了W”,但是什么也没有发生。这是代码:

public class SO extends JFrame {

    public static void main(String[] args) {
        new SO();
    }
    C c;
    public SO(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        c=new C();
        c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "wp");
        c.getActionMap().put("wp", new Wp());
        this.setVisible(true);
    }
    private class C extends JComponent {
        public void paint(Graphics g){}
    }
    private class Wp extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("W pressed");
        }

    }

}

1 个答案:

答案 0 :(得分:2)

使用操作像component.getActionMap().put("doSomething", anAction);一样拨打电话 有关更多信息,请参考Key Binding。以下是我在另一个Stackoverflow问题参考链接SO Ref link

中引用的示例代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonBinding {

    private JPanel contentPane;
    private JTextField tField;
    private JButton button;
    private KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");

    private Action action = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("Action Performed");
            contentPane.setBackground(Color.BLUE);
        }
    };

    private MouseAdapter mouseActions = new MouseAdapter() {
        @Override
        public void mouseEntered(MouseEvent me) {
            System.out.println("Mouse Entered");
            JButton button = (JButton) me.getSource(); 
            button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "enter");
            button.getActionMap().put("enter", action);
        }

        @Override
        public void mouseExited(MouseEvent me) {
            System.out.println("Mouse Exited");
            JButton button = (JButton) me.getSource();
            button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "none");
            contentPane.setBackground(Color.RED);
        }
    };  

    private void displayGUI() {
        JFrame frame = new JFrame("Button Binding Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setOpaque(true);
        tField = new JTextField(10);
        button = new JButton("Click Me");
        button.addMouseListener(mouseActions);

        contentPane.add(tField);
        contentPane.add(button);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new ButtonBinding().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}