简体   繁体   English

键绑定未调用AbstractAction

[英]Keybinding not calling AbstractAction

I am making a game where the user has to press keys to move around. 我正在制作一个游戏,用户必须按下按键才能四处移动。 I am using keybindings but they are not working. 我正在使用键盘绑定,但是它们不起作用。 The keybindings are supposed to call the Wp class and print "W pressed", but nothing happens. 这些键绑定应该调用Wp类并显示“按了W”,但是什么也没有发生。 Here's the code: 这是代码:

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");
        }

    }

}

Use Action to call like component.getActionMap().put("doSomething", anAction); 使用Action调用类似component.getActionMap().put("doSomething", anAction); Refer Key Binding for more information. 有关更多信息,请参见键绑定 Below is a sample code I have referred in another Stackoverflow Question reference link SO Ref link 以下是我在另一个Stackoverflow问题参考链接中引用的示例代码SO Ref链接

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);
    }
}

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

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