我试图根据用户的键输入向上,向下,向左和向右移动一个矩形。我的KeyHandler工作正常,它将向上或向下移动形状,但不是根据X轴(向左或向右)。
以下是我的KeyHandler(KeyPressed)的代码。
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
System.out.println("You have pressed: " +keyCode);
_screen.player[0].y -= 3;
_screen.repaint();
}else if(keyCode == KeyEvent.VK_DOWN){
System.out.println("You have pressed: " +keyCode);
_screen.player[0].y += 3;
_screen.repaint();
}else if(keyCode == KeyEvent.VK_LEFT){
System.out.println("You have pressed: " +keyCode);
_screen.player[0].x -= 3;
_screen.repaint();
}else if(keyCode == KeyEvent.VK_RIGHT){
System.out.println("You have pressed: " +keyCode);
_screen.player[0].x += 3;
_screen.repaint();
}else if(keyCode == KeyEvent.VK_ESCAPE){
System.exit(0);
}
}
在X轴上移动矩形有什么不同吗?
答案 0 :(得分:2)
发布的代码中没有明显的错误。问题可能出在未显示的代码部分。发布可运行的示例(即SSCCE)总是一个好主意。
使用键侦听器时,请确保该组件具有焦点并具有焦点。但是,建议不要使用键侦听器,因为它是一个较低级别的接口。最好使用Key Bindings。有关详细信息和示例,请参阅How to Use Key Bindings。另请参阅Swing actions。
这是一个简单的示例,演示了在面板周围移动矩形的键绑定和操作:
public class MoveRectangle extends JPanel {
private Point rectPosition = new Point();
public static final int RECT_SIZE = 50;
public Point getRectPosition() {
return rectPosition;
}
public void setRectPosition(Point position) {
this.rectPosition = position;
repaint();
}
public MoveRectangle() {
InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "up");
actionMap.put("up", new MoveAction(this, MoveAction.Direction.UP));
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "down");
actionMap.put("down", new MoveAction(this, MoveAction.Direction.DOWN));
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "left");
actionMap.put("left", new MoveAction(this, MoveAction.Direction.LEFT));
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");
actionMap.put("right", new MoveAction(this, MoveAction.Direction.RIGHT));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(rectPosition.x, rectPosition.y, RECT_SIZE, RECT_SIZE);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
static class MoveAction extends AbstractAction {
public enum Direction{
LEFT, RIGHT, UP, DOWN
};
private Direction direction;
private MoveRectangle panel;
public MoveAction(MoveRectangle panel, Direction direction) {
this.direction = direction;
this.panel = panel;
}
@Override
public void actionPerformed(ActionEvent e) {
Point position = panel.getRectPosition();
switch(direction) {
case LEFT:
position.x -= 3;
break;
case RIGHT:
position.x += 3;
break;
case UP:
position.y -= 3;
break;
case DOWN:
position.y += 3;
break;
}
panel.setRectPosition(position);
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Move Rectangle");
frame.getContentPane().add(new MoveRectangle());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
}