我的 KeyBindings 不起作用,我想知道为什么

My KeyBindings don't work and I want to know why

我刚刚开始学习如何使用 keyBindings,但我无法找出我做错了什么,因为当我按下键盘上的向上箭头时,它什么也没做。

我的主游戏Window

public class GameWindow extends JFrame{

private static final long serialVersionUID = 1L;

public int WIDTH = 160, HEIGHT = WIDTH/12 *9, SCALE = 3;

public boolean running = false;

BackGround bg = new BackGround();

Ranger R = new Ranger();

TimerClass T = new TimerClass();

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

}

public GameWindow() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(WIDTH * SCALE, HEIGHT * SCALE);
    setResizable(false);

    running = true;

    add(bg);
    bg.add(R);

    bg.setFocusable(true);

    R.setFocusable(true);

    setFocusable(true);
    setVisible(true);
    bg.repaint();

    run();
}

public void run() {
    while (running) {
        render();
    }
}

public void render() {
    bg.setLocation(Ranger.bgX, Ranger.bgY);
    R.setLocation(Ranger.X, Ranger.Y);
    R.setIcon(Ranger.rangerA[Ranger.I]);
    R.repaint();
    bg.repaint();
}

}

还有我的游侠Class

public class Ranger extends JLabel {

private static final long serialVersionUID = 1L;

public static int X, Y, dX, dY, bgX, bgY, I = 0, jumpTime = 100;

public static boolean moving = false, movingLeft = false,
        movingRight = false, onFloor = false, jumping = false,
        movingUp = false, movingDown = false;

public int totalImages = 6;

public BufferedImage ranger1, ranger2, ranger3, ranger4, ranger5, ranger6;

public static ImageIcon[] rangerA;

static TileMap TileMap = new TileMap();

public Ranger() {

    try {
        // not moving
        ranger1 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger0.png"));
        ranger2 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger1.png"));
        // moving Left
        ranger3 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger2.png"));
        ranger4 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger3.png"));
        // moving Right
        ranger5 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger4.png"));
        ranger6 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger5.png"));

    } catch (IOException e) {
        e.printStackTrace();
    }

    array();

}

public void array() {
    rangerA = new ImageIcon[6];
    {
        rangerA[0] = new ImageIcon(ranger1);
        rangerA[1] = new ImageIcon(ranger2);
        rangerA[2] = new ImageIcon(ranger3);
        rangerA[3] = new ImageIcon(ranger4);
        rangerA[4] = new ImageIcon(ranger5);
        rangerA[5] = new ImageIcon(ranger6);
    }
}

public void move() {

    AbstractAction moveUp = new AbstractAction() {

        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            System.out.println("Move up");
        }

    };

    this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "moveUp");
    this.getActionMap().put("moveUp", moveUp);

    X += dX;
    Y += dY;

    dX = 0;
    dY = 0;

    if (movingRight || movingLeft) {
        moving = true;
    }

}

}

我正在尝试输出它将在控制台中向上移动的信息,以便我可以了解如何制作新的 KeyBindings,但我不知道为什么它不起作用。任何 solutions/tips 将不胜感激。

P.S。我是 Java 的新手,很抱歉犯了一些简单的错误,我也知道主游戏中的疯狂循环 window class.

编辑:在单独的计时器中每隔几毫秒调用一次 move() class。

KeyStroke.getKeyStroke("W") 并不像您想象的那样。使用此形式需要对操作进行详细描述,即 typed w

因此,我从不使用它。相反,我更喜欢使用更直接的 KeyStroke.getKeyStroke(KeyEvent.VK_W, 0)

有关详细信息,请参阅 KeyStroke#getKeyStroke(int, int)

您也没有绑定击键,因为您从不调用 move 方法。相反,在 类 构造函数或其他一些只应调用一次的方法中绑定击键。