发布的代码中没有明显的错误。问题可能出在未显示的代码部分。发布一个可运行的示例(即SSCCE)总是一个好主意。
使用关键侦听器时,请确保组件可聚焦且具有焦点。但是,不建议使用键侦听器,因为它是较低级别的接口。最好改用键绑定。有关详细信息和示例,请参阅如何使用键绑定。还看Swing动作。
这是一个简单的示例,演示了在面板周围移动矩形的键绑定和操作:
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();
}
});
}
}