【问题标题】:Why can't I move a rectangle using navigation keys in Java?为什么我不能在 Java 中使用导航键移动矩形?
【发布时间】:2016-07-12 16:48:14
【问题描述】:

嘿,伙计们,当我向左或向右击球时,我正试图让我的 Player 类移动,但没有任何反应/我是 java 新手,我真的不明白为什么它不起作用。我没有错误,但仍然没有任何反应。谢谢你的帮助。另外,如果您能向我解释为什么它不起作用,那将非常有帮助。

import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.EventQueue;
        import java.awt.Graphics;
        import java.awt.Rectangle;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import java.awt.event.ComponentAdapter;
        import java.awt.event.ComponentEvent;
        import java.awt.event.KeyEvent;
        import java.awt.event.KeyListener;
        import javax.swing.JFrame;
        import static javax.swing.JFrame.EXIT_ON_CLOSE;
        import javax.swing.JPanel;
        import javax.swing.Timer;
        import javax.swing.UIManager;
        import javax.swing.UnsupportedLookAndFeelException;

        public class GamePanel extends JPanel implements ActionListener, KeyListener {

            public final static int WIDTH = 700, HEIGHT = 450;

            public Game game;
            public Player player;

            public GamePanel(Game game) {
                setBackground(Color.WHITE);
                this.game = game;
                addKeyListener(this);
                setFocusable(true);
            }
                    public void update() {
                player.update();
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(WIDTH, HEIGHT);
            }

            public void init() {
                System.out.println("!!");
                player = new Player(this, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, getHeight() - 50, getWidth() / 2);
                repaint();
            }


            public void actionPerformed(ActionEvent e) {
                update();
                repaint();
            }

            public Player getPlayer(int playerNo) {
                return player;
            }
      public void keyPressed(KeyEvent e) {
            player.pressed(e.getKeyCode());
        }

        public void keyReleased(KeyEvent e) {
            player.released(e.getKeyCode());
        }

        public void keyTyped(KeyEvent e) {
    ;
        }

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (player != null) {
                    player.paint(g);
                }
            }
        }

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Player {

        public static final int WIDTH = 50, HEIGHT = 50;
        public GamePanel game;
        public int left, right;
        public int y;
        public int x, xa;

        public Player(GamePanel game, int left, int right, int y, int x) {
            this.game = game;
            this.left = left;
            this.right = right;
            this.x = x;
            y = game.getHeight() - HEIGHT;
            x = game.getWidth() / 2;
        }

        public void update() {
            if (x > 0 && x < game.getWidth() - WIDTH) {
                x += xa;
            } else if (x == 0) {
                x++;
            } else if (x == game.getWidth() - WIDTH) {
                x--;
            }
        }

 public void pressed(int keyCode) {
        if (keyCode == left)
            xa = -1;
        else if (keyCode == right)
            xa = 1;
    }

    public void released(int keyCode) {
        if (keyCode == left || keyCode == right)
            xa = 0;
    }

        public Rectangle getBounds() {
            return new Rectangle(x, y, WIDTH, HEIGHT);
        }

        public void paint(Graphics g) {
            System.out.println(x + "x" + y);
            g.fillRect(x, y, WIDTH, HEIGHT);
            g.setColor(Color.ORANGE);
        }
    }

【问题讨论】:

    标签: java swing awt keylistener


    【解决方案1】:

    您只需要在 keyReleased() 方法的末尾添加一个repaint(); 语句。无论您是在 keyTyped() 还是 keyPressed() 方法中执行操作,只要您释放钥匙。所以程序控件在这里应该会遇到repaint()语句。

    @Override
    public void keyReleased(KeyEvent e){
        player.released(e.getKeyCode());
        repaint(); //<--- THIS HERE
    }
    

    希望这能解决你的问题。


    还请注意@SasonOhanian 的回答并改进您的代码。

    【讨论】:

      【解决方案2】:

      您在 actionPerformed 回调中有更新和重绘逻辑,而 KeyPressed 为玩家设置移动变量。在您的侦听器方法中调试或打印某些内容以查看调用方式。我不认为 actionPerformed 和 KeyPressed 会被称为你期望的方式和顺序。

      【讨论】:

        【解决方案3】:

        Ty 提供所有帮助,但我需要使 x = x + 1 和 x = x -1

        【讨论】:

          猜你喜欢
          • 2022-06-29
          • 1970-01-01
          • 1970-01-01
          • 2015-04-13
          • 1970-01-01
          • 2019-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多