1

我正在尝试根据用户的按键输入向上、向下、向左和向右移动一个矩形。我的 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 轴上移动一个矩形是否需要做一些不同的事情?

4

1 回答 1

2

发布的代码中没有明显的错误。问题可能出在未显示的代码部分。发布一个可运行的示例(即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();
            }
        });
    }
}
于 2013-03-30T16:21:01.800 回答