Java 键绑定(bind)

标签 java swing key-bindings

我有一个任务要为 uni 做,需要让用户使用方向键控制游戏。

到目前为止,我得到了以下结果,但这不起作用。有什么明显的我遗漏的吗?

    // key bindings

    // add the key bindings for up, down, left and right to the input map
    gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "down");
    gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "up");
    gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0), "left");
    gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "right");

    // assign actions to the key bindings in the action map
    gamePanel.getActionMap().put("down", new AbstractAction() 
    {
        public void actionPerformed(ActionEvent event)
        {
            move("DOWN");
        }
    });
    gamePanel.getActionMap().put("up", new AbstractAction() 
    {
        public void actionPerformed(ActionEvent event)
        {
            move("UP");
        }
    });
    gamePanel.getActionMap().put("left", new AbstractAction() 
    {
        public void actionPerformed(ActionEvent event)
        {
            move("LEFT");
        }
    });
    gamePanel.getActionMap().put("right", new AbstractAction() 
    {
        public void actionPerformed(ActionEvent event)
        {
            move("RIGHT");
        }
    });

按下任何方向按钮时没有任何反应。

预先感谢您的帮助。

下面的 MCVE - 我做的第一个所以让我知道它是否足够好

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import com.sun.java.swing.plaf.windows.resources.windows;

import java.util.ArrayList;

public class MCVE extends JFrame
{
    // Game panel
    private JPanel gamePanel;

    private Container window;

    public static void main(String[] args) 
    {
        MCVE frame = new MCVE();

        frame.setSize(1000,700);

        frame.createGUI();

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);
    }

    private void createGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        window = getContentPane();

        gamePanel = new JPanel();


        window.add(gamePanel);
    }

    private void keyBindings()
    {
        // key bindings

        // add the key bindings for up, down, left and right to the input map
        gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "down");
        gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "up");
        gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0), "left");
        gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "right");

        // assign actions to the key bindings in the action map
        gamePanel.getActionMap().put("down", new AbstractAction() 
        {
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("down");
            }
        });
        gamePanel.getActionMap().put("up", new AbstractAction() 
        {
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("up");
            }
        });
        gamePanel.getActionMap().put("left", new AbstractAction() 
        {
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("left");
            }
        });
        gamePanel.getActionMap().put("right", new AbstractAction() 
        {
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("right");
            }
        });
    }
}

最佳答案

您的可运行示例永远不会调用 keyBindings 方法,因此它们永远不会被注册...

private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    window = getContentPane();
    gamePanel = new JPanel();
    window.add(gamePanel);
    // This is going to help...
    keyBindings();
}

这就是调试语句和投入少量时间调试您的代码会有所帮助的地方。当某些东西不起作用时,请始终先检查您是否已正确设置...我仍然经常犯这种错误;)

为了以防万一,此示例使用 onKeyRelease 参数来监视按下和释放事件以及支持数字键盘箭头键

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel up;
        private JLabel down;
        private JLabel left;
        private JLabel right;

        public TestPane() {

            up = createLabel("UP");
            down = createLabel("DOWN");
            left = createLabel("LEFT");
            right = createLabel("RIGHT");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.CENTER;
            add(up, gbc);
            gbc.gridy = 2;
            add(down, gbc);

            gbc.gridx = 0;
            gbc.gridy = 1;
            add(left, gbc);
            gbc.gridx = 2;
            add(right, gbc);

            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "up-press", new HighlightAction(up, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_UP, 0, false), "up-press", new HighlightAction(up, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "down-press", new HighlightAction(down, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_DOWN, 0, false), "down-press", new HighlightAction(down, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "left-press", new HighlightAction(left, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0, false), "left-press", new HighlightAction(left, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right-press", new HighlightAction(right, true));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0, false), "right-press", new HighlightAction(right, true));

            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "up-release", new HighlightAction(up, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_UP, 0, true), "up-release", new HighlightAction(up, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "down-release", new HighlightAction(down, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_DOWN, 0, true), "down-release", new HighlightAction(down, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "left-release", new HighlightAction(left, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_LEFT, 0, true), "left-release", new HighlightAction(left, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "right-release", new HighlightAction(right, false));
            registerKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_KP_RIGHT, 0, true), "right-release", new HighlightAction(right, false));
        }

        public void registerKeyBinding(KeyStroke keyStroke, String name, Action action) {
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            im.put(keyStroke, name);
            am.put(name, action);
        }

        public JLabel createLabel(String text) {
            JLabel label = new JLabel(text);
            label.setOpaque(true);
            label.setHorizontalAlignment(JLabel.CENTER);
            return label;
        }

        public class HighlightAction extends AbstractAction {

            private JLabel label;
            private boolean on;

            public HighlightAction(JLabel label, boolean on) {
                this.label = label;
                this.on = on;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                if (on) {
                    label.setBackground(Color.RED);
                    label.repaint();
                } else {
                    label.setBackground(null);
                }
            }

        }

    }

}

关于Java 键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28267271/

相关文章:

java - 新手 GridBagLayout 问题

Java 键绑定(bind)在按键时输出到控制台

java - Java中如何在按钮中设置单键助记符?

java - JavaFX 属性中 Value 和 ObjectValue 有什么区别?

java - 如何修复 : Error creating bean with name : Unsatisfied dependency expressed through field

java - 如何使用二维按钮阵列刷新面板?

Java 键绑定(bind)不工作

java - JAVA如何从文件中读取这些数据

java - JVM 消耗 100% CPU,大量 GC

java - 小程序在浏览器中显示为空