Java 为什么赢了';不能调用按键方法吗?

Java 为什么赢了';不能调用按键方法吗?,java,user-interface,keylistener,keyevent,Java,User Interface,Keylistener,Keyevent,我正在用Java为一个项目制作一个格斗游戏,并试图通过键盘(keyEvents)在面板上移动和重新绘制一张图片。我试图通过在keyPressed方法中添加一个开关来实现这一点,同时将keyListener添加到面板中。我一直在遵循我的Java书中的一个例子,我写的代码几乎一样,但它就是不起作用 我真正想知道的是为什么它对关键事件根本没有反应。程序编译得很好,但什么也没发生。我不知道出了什么问题。如果我做了一个,它不会到达keyPressed()方法中的断点,如果我把它放在那里,它也不会执行pri

我正在用Java为一个项目制作一个格斗游戏,并试图通过键盘(keyEvents)在面板上移动和重新绘制一张图片。我试图通过在keyPressed方法中添加一个开关来实现这一点,同时将keyListener添加到面板中。我一直在遵循我的Java书中的一个例子,我写的代码几乎一样,但它就是不起作用

我真正想知道的是为什么它对关键事件根本没有反应。程序编译得很好,但什么也没发生。我不知道出了什么问题。如果我做了一个,它不会到达
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());
  }

可能帧正在捕捉事件。。。你确定面板是焦点吗?把这样一个监听器也放在框架上,看看它是否被调用:)你的代码对我来说似乎工作得很好。您确定没有其他组件具有焦点吗?我如何检查是否存在这种情况?当我将侦听器添加到面板时,框架如何做到这一点?真是不知所措,甚至我的教授也不明白为什么这样做行不通:(我用if(this.isFocusable()==true)backbackground(Color.black);为了看它是否是可聚焦的,我也用了这个。requestFocus();在构造函数中查看这是否是问题所在,尽管我不能确定。我真的不习惯这里的注释系统,我在尝试添加新行时不断注释:P我将尝试向帧中添加侦听器。我尝试使用if(gameFrame.isFocusOwner()==true){system.out.println(“ppifjlos”);},因为Eclipse说hasFocus()已过时。尝试测试帧是否具有焦点,但它似乎没有。还尝试在面板上执行此操作,但它也没有在面板上执行任何操作,因此它似乎没有焦点。建议使用requestFocusInWindow()而不是requestFocus():)我尝试添加movePanel.requestFocus();我创建了MovePanel,但它没有做任何事情,而且它仍然无法通过我在MovePanel的构造函数中使用此.isFocusOwner()进行的检查。我不知道为什么它不会聚焦,因为isFocusable()方法会产生true:(@Suresh S我向听众添加了跟踪,什么都没发生。像我这样的scrub程序员竟然能把事情搞砸,真是不可思议:P@Suresh-S我将侦听器添加到帧中,似乎确实是该侦听器以某种方式窃取了焦点,因为跟踪在那里发生了反应。这有什么帮助吗?@Clearout:如果帧中有侦听器,请将其注释掉,然后重试尝试