为什么我不能使用 Java 中的导航键移动矩形?
Why can't I move a rectangle using navigation keys in Java?
大家好,我试图让我的播放器 class 在我向左或向右击打时移动,但没有任何反应/我是 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);
}
}
您在 actionPerformed 回调中有更新和重绘逻辑,而 KeyPressed 为玩家设置移动变量。在您的侦听器方法中调试或打印某些内容以查看调用方式。我不认为 actionPerformed 和 KeyPressed 会按照您期望的方式和顺序被调用。
只需要在keyReleased()方法的末尾添加repaint();
语句即可。无论您是在 keyTyped() 还是 keyPressed() 方法中执行某些操作,keyReleased() 方法总是在您释放键时调用。所以程序控制在这里应该会遇到repaint()语句。
@Override
public void keyReleased(KeyEvent e){
player.released(e.getKeyCode());
repaint(); //<--- THIS HERE
}
希望这能解决您的问题。
另请注意@SasonOhanian 的回答并改进您的代码。
感谢所有帮助,但我需要使 x = x + 1 和 x = x -1
大家好,我试图让我的播放器 class 在我向左或向右击打时移动,但没有任何反应/我是 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);
}
}
您在 actionPerformed 回调中有更新和重绘逻辑,而 KeyPressed 为玩家设置移动变量。在您的侦听器方法中调试或打印某些内容以查看调用方式。我不认为 actionPerformed 和 KeyPressed 会按照您期望的方式和顺序被调用。
只需要在keyReleased()方法的末尾添加repaint();
语句即可。无论您是在 keyTyped() 还是 keyPressed() 方法中执行某些操作,keyReleased() 方法总是在您释放键时调用。所以程序控制在这里应该会遇到repaint()语句。
@Override
public void keyReleased(KeyEvent e){
player.released(e.getKeyCode());
repaint(); //<--- THIS HERE
}
希望这能解决您的问题。
另请注意@SasonOhanian 的回答并改进您的代码。
感谢所有帮助,但我需要使 x = x + 1 和 x = x -1