gpt4 book ai didi

java - 为什么不调用 keyPressed 方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:20 24 4
gpt4 key购买 nike

我正在为一个项目用 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() 方法没有反应?

最佳答案

我没有看到下面的代码

您应该在创建 MovePanel 实例的地方调用下面的行

 MovePanel.requestFocus();      // Give the panel focus.



public class demo extends JFrame
{
MovePanel panel;

public demo ()
{
panel= new MovingTextPanel();
this.getContentPane().setLayout(new BorderLayout())
this.setTitle("Demo");
this.pack();
panel.requestFocus(); // Give the panel focus.
}
}

在您的 MovePanel 中将 setFocusable 添加为 true

 public MovePanel(GameFrame gameFrame) {

this.setFocusable(true); // Allow this panel to get focus.
// Adding the key listener here.
addKeyListener(this);

一些痕迹

- Characters (a, A, #, ...) - handled in the keyTyped() listener.
- Virtual keys (arrow keys, function keys, etc) - handled with keyPressed() listener.
- Modifier keys (shift, alt, control, ...) - Usually their status (up/down) is tested by calls in one of the other listeners, rather than in keyPressed().

public void keyTyped(KeyEvent e) {
System.out.println(e.toString());
}
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.toString());
}

关于java - 为什么不调用 keyPressed 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com