简体   繁体   English

我不能通过键绑定移动我的JLabel吗?

[英]I can't move my JLabel with Key Binding?

I am trying to make a game in Java where pressing spacebar moves the box in the window. 我正在尝试用Java制作游戏,按空格键可在窗口中移动该框。 I am using Key Binding to accomplish this task. 我正在使用“键绑定”来完成此任务。 The problem is that I can't figure out how to use an ActionListener on the box itself, which is a JLabel. 问题是我不知道如何在包装盒本身(即JLabel)上使用ActionListener。 Here is the code below: 这是下面的代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction; 
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Game {

private static JFrame frame     = new JFrame();
private static JPanel gamePanel = new JPanel();
private static Action playerAction = new PlayerListener();
private static JLabel box = new JLabel();
private static int x = 250;
private static int y = 250;

public static void main(String[] args) {

    frame.add(boxPanel());
    frame.setTitle("Block Game");
    frame.setSize(500,500);
    frame.setLocationRelativeTo(null);
    frame.setFocusable(true);

    box.addActionListener(playerAction);

    frame.setVisible(true);

}

static JPanel boxPanel() {
    ImageIcon boxIcon = new ImageIcon("box.png");
    box.setIcon(boxIcon);

    box.setSize(30,30);
    box.setLocation(x,y);

    box.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doPlayerAction");
    box.getActionMap().put("doPlayerAction", playerAction);

    gamePanel.setLayout(null);
    gamePanel.add(box);
    frame.add(gamePanel);

    return gamePanel;   
}

static class PlayerListener extends AbstractAction {

    public void actionPerformed(ActionEvent e) {

        System.out.println("SPACEBAR");

    }

}
}

I tried changing the box to a JButton and working with that instead, but I have found that "SPACEBAR" only prints out when I click on the box itself. 我尝试将框更改为JButton并改为使用它,但是我发现只有在单击框本身时才显示“ SPACEBAR”。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

Your "core" problem revolves around box.getInputMap() , change it to something more like box.getInputMap(WHEN_IN_FOCUSED_WINDOW) which will mean that the API will respond to key events whenever the window has focus, regardless what other components might have focus. 您的“核心”问题围绕着box.getInputMap() ,将其更改为类似于box.getInputMap(WHEN_IN_FOCUSED_WINDOW) ,这意味着只要窗口获得焦点,API就会响应按键事件,而与其他组件无关。

I'd also suggestsomething more like box.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "doPlayerAction") , as the mechanism which KeyStroke uses to parse String to a KeyStroke is more complicated then it might seem, normally requiring additional information like pressed , released or typed , it's just easier to use the virtual keys 我还建议像box.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "doPlayerAction") ,因为KeyStroke用于将String解析为KeyStroke的机制比较复杂,可能会更复杂看起来,通常需要其他信息,例如pressedreleasedtyped ,使用虚拟键更容易

I would also bind the keys to the gamePanel as a general preference, as it should be the container making decisions about what to do, but that's just me. 我还通常将按键绑定到gamePanel ,因为它应该是容器来决定要做什么,但这就是我自己。

Have a look at How to Use Key Bindings for more details 看看如何使用键绑定了解更多详细信息

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

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