为什么 keyPressed 方法不会被调用?
我正在为一个项目用 Java 制作一个格斗游戏,并试图让图片移动并在面板上重新绘制,以响应键盘(keyEvents)。我试图通过在 keyPressed 方法中设置一个开关,同时将 keyListener 添加到面板来实现此目的。我一直在遵循我的 Java 书中的一个示例,我编写的代码几乎相同,但它不起作用。
我真正想知道的是为什么它似乎根本没有对 keyEvents 做出反应。程序编译得很好,但什么也没发生。我不知道出了什么问题。如果我创建一个,它不会到达 keyPressed()
方法中的断点,如果我将其放入其中,它也不会执行 println()
。因此 keyPressed()
方法根本没有反应。我还测试并确保面板可聚焦,因此我确定它具有键盘焦点。
public class MovePanel extends JPanel implements KeyListener {
private ImageIcon currentImage, facingLeft, facingRight;
private int position;
private final int MOVEMENT;
private GameFrame gameFrame;
private URL lefturl, righturl;
public MovePanel(GameFrame gameFrame) {
// Taking in a gameFrame to be able to swap the active panel
// (not really relevant).
this.gameFrame = gameFrame;
// Adding the key listener here.
addKeyListener(this);
// These are just the Images I'm using to test.
// Trying to get it to swap from one to the other.
lefturl = getClass().getResource("/Images/facingLeft.jpg");
righturl = getClass().getResource("/Images/facingRight.jpg");
facingLeft = new ImageIcon(lefturl);
facingRight = new ImageIcon(righturl);
currentImage = facingLeft;
position = 50;
MOVEMENT = 30;
setBackground(Color.red);
setPreferredSize(new Dimension(600,300));
// Calling this method so that the panel will react
// to the keyboard without having to be clicked.
setFocusable(true);
}
// This is just the paintComponent method which works fine to paint the image
// when starting the game.
public void paintComponent(Graphics page) {
super.paintComponent(page);
currentImage.paintIcon(this, page, position, 170);
}
// No matter what I try to do inside the keyPressed method
// it doesnt seem to react at all.
public void keyPressed(KeyEvent e) {
// This switch is to make the method react accordingly to the keys pressed.
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
// Here I'm changing the "active" image and the position
// by changing the position variable which is used
// to determine the x placement of the image.
// This case is suppused to react if the left arrow key is pressed.
currentImage = facingRight;
position -= MOVEMENT;
break;
case KeyEvent.VK_RIGHT:
currentImage = facingRight;
position += MOVEMENT;
break;
// This case is to exit to the menu when escape is pressed.
case KeyEvent.VK_ESCAPE:
gameFrame.setMenuPanelActive();
break;
}
// After reacting to any of the proper keys pressed
// I'm trying to repaint which will use the
// paintComponent method to paint the new image in its new position.
repaint();
}
// I have empty definitions for the other
// implemented methods but won't be posting them.
}
有谁知道为什么这不起作用?为什么 keyPressed()
方法没有反应?
I'm making a fighting game in Java for a project and trying to get a picture to move and be repainted across the panel reacting to the keyboard (keyEvents). I'm trying to accomplish this by having a switch in the keyPressed method, while adding the keyListener to the panel. I have been following an example in my Java book and the code I've written as nearly the same yet it just won't work.
What I'm really wondering is why it doesnt seem to react to keyEvents at all. The program compiles fine and all, yet nothing happens. I have no idea what is going wrong. It doesn't reach the breakpoint in the keyPressed()
method if I make one, nor will it it do a println()
if I put it in there. So the keyPressed()
method doesnt react at all. I have also tested and made sure that the panel is focusable so I'm sure it has the keyboard focus.
public class MovePanel extends JPanel implements KeyListener {
private ImageIcon currentImage, facingLeft, facingRight;
private int position;
private final int MOVEMENT;
private GameFrame gameFrame;
private URL lefturl, righturl;
public MovePanel(GameFrame gameFrame) {
// Taking in a gameFrame to be able to swap the active panel
// (not really relevant).
this.gameFrame = gameFrame;
// Adding the key listener here.
addKeyListener(this);
// These are just the Images I'm using to test.
// Trying to get it to swap from one to the other.
lefturl = getClass().getResource("/Images/facingLeft.jpg");
righturl = getClass().getResource("/Images/facingRight.jpg");
facingLeft = new ImageIcon(lefturl);
facingRight = new ImageIcon(righturl);
currentImage = facingLeft;
position = 50;
MOVEMENT = 30;
setBackground(Color.red);
setPreferredSize(new Dimension(600,300));
// Calling this method so that the panel will react
// to the keyboard without having to be clicked.
setFocusable(true);
}
// This is just the paintComponent method which works fine to paint the image
// when starting the game.
public void paintComponent(Graphics page) {
super.paintComponent(page);
currentImage.paintIcon(this, page, position, 170);
}
// No matter what I try to do inside the keyPressed method
// it doesnt seem to react at all.
public void keyPressed(KeyEvent e) {
// This switch is to make the method react accordingly to the keys pressed.
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
// Here I'm changing the "active" image and the position
// by changing the position variable which is used
// to determine the x placement of the image.
// This case is suppused to react if the left arrow key is pressed.
currentImage = facingRight;
position -= MOVEMENT;
break;
case KeyEvent.VK_RIGHT:
currentImage = facingRight;
position += MOVEMENT;
break;
// This case is to exit to the menu when escape is pressed.
case KeyEvent.VK_ESCAPE:
gameFrame.setMenuPanelActive();
break;
}
// After reacting to any of the proper keys pressed
// I'm trying to repaint which will use the
// paintComponent method to paint the new image in its new position.
repaint();
}
// I have empty definitions for the other
// implemented methods but won't be posting them.
}
Does anyone have any idea why this doesn't work? Why keyPressed()
method won't react?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到如下代码,
您应该调用下面的行,在其中创建 MovePanel 实例
在您的 MovePanel 中将 setFocusable 添加为 true
更多跟踪
I dont see the code like below
you should call the below line where u create the instance of MovePanel
In your MovePanel add setFocusable to true
Some more traces