简体   繁体   English

为什么没有检测到按键?

[英]Why aren't keys being detected?

I have a Pong game and am trying to figure out how to detect a couple of keys. 我有一个Pong游戏,正在尝试弄清楚如何检测几个键。 Here is my paddle class: 这是我的桨课程:

import java.awt.geom.*;

public class Paddle extends Rectangle2D.Float{
    public Paddle(int x, int y){
        super.x = x;
        super.y = y;
        super.width = w;
        super.height = h;
    }
}

Here is the PongBall class: 这是PongBall课:

import java.awt.geom.*;

public class PongBall extends Ellipse2D.Float{
    public PongBall(int x, int y, int w, int h){
        super.x = x;
        super.y = y;
        super.width = w;
        super.height = h;
    }
}

Here is the class/area that the paddle and ball will be on: 这是桨和球所在的类/区域:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.JComponent;

public class PongPlayArea extends JComponent implements KeyListener{

public int width, height;

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    PongPlayArea ppa = new PongPlayArea(600, 600);
    Timer t = new Timer(20, e -> {ppa.repaint();});
    t.start();

    frame.add(ppa);
    frame.setVisible(true);
}
public PongPlayArea(int width, int height){
    this.width = width;
    this.height = height;
    this.addKeyListener(this);
}
public void paintComponent(Graphics g){

    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.BLACK);
    g2.draw(new Rectangle2D.Float(0, 0, width, height));
    g2.fill(getVisibleRect());

    g2.setColor(Color.WHITE);
    g2.draw(new Paddle());
}
public Dimension getPreferredSize(){
    return new Dimension(width, height);
}
public void keyPressed(KeyEvent e){
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
        System.out.println("Success");
    }
}
public void keyReleased(KeyEvent e){

}
public void keyTyped(KeyEvent e){

}
}

My problem is that when ever the key is pressed (in this case, the right arrow key) nothing is happening. 我的问题是,每当按下该键(在这种情况下为向右箭头键)时,什么都没有发生。 I don't know what is wrong. 我不知道怎么了 Does anyone have a solution? 有没有人有办法解决吗?

EDIT: ANSWER: As suggested by camickr, Instead of use KeyListener, I have decided to use key binding, which is much more efficient. 编辑:答复:正如camickr所建议的,我决定使用键绑定,而不是使用KeyListener,它效率更高。

You need to call .addKeyListener() to register your KeyListener implementation. 您需要调用.addKeyListener()来注册KeyListener实现。 Call it on the component on which you want to listen for key events. 在要侦听关键事件的组件上调用它。 Probably this.addKeyListener(this); 可能this.addKeyListener(this); . I would make your KeyListener implementation a separate class. 我将使您的KeyListener实现成为一个单独的类。

I think that the better solution is to use KeyboardFocusManager . 我认为更好的解决方案是使用KeyboardFocusManager You may have problems (as above) with KeyListeners, because it requres to be focused, you have to register Listener and add methods to your PongPlayArea class (which becomes very long) etc. One I had problems similar to your, and the best solution I found was KeyboardFocusManager . 您可能会遇到KeyListeners的问题(如上所述),因为它需要重点关注,因此您必须注册Listener并将方法添加到PongPlayArea类(这会变得很长)等。我遇到了与您类似的问题,并且是最佳的解决方案我发现是KeyboardFocusManager To your constructor: 给您的构造函数:

KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());

And create class: 并创建类:

private class GameDispatcher implements KeyEventDispatcher {

    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == 82) {
            System.out.println("Success");
        }
        return false;
    }

}

Check if it works if you want and tell me is it solved your problems. 检查是否可以运行,并告诉我它是否解决了您的问题。

As said by camickr, key bindings is the way to go. 正如camickr所说,键绑定是必经之路。 Do not use key listeners. 不要使用键监听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM