我在添加关键监听器时犯了什么错误?

时间:2014-04-22 20:46:32

标签: java swing jpanel paintcomponent keylistener

我的发射器对象(由秃鹰图片代表)对于我正在制作的游戏没有响应关键事件。我知道我是如何宣布听众的问题,但我不确定我做错了什么。

这是我的代码:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    GameTest t = new GameTest();
}

public static class GameTest extends JFrame {

    private static final int WINDOW_WIDTH = 800;
    private static final int WINDOW_HEIGHT = 500;
    private GamePanel gamePanel;
    private GameTest gameTest;

    public GameTest() throws IOException {
        super("Deep Fried Freedom");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setLayout(new BorderLayout());
        gamePanel = new GamePanel();
        add(gamePanel);
        center(this);
        setVisible(true);
        this.addKeyListener(new aKeyListener());
        this.setFocusable(true);
    }

    public void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }//end of center method  

    public class aKeyListener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
        }//end empty keyTyped method

        @Override
        public void keyPressed(KeyEvent e) {
            Launcher.lRun -= 5;
            gamePanel.move();
        }//end keyPressed method

        @Override
        public void keyReleased(KeyEvent e) {
        }//end empty keyReleased method

    }//end aKeyListener class

}//end GameTest class
}// end main class


public class GamePanel extends JPanel {
Launcher launcher1;
Background bground1;

public GamePanel() throws IOException {
    super();
    launcher1 = new Launcher();
    bground1 = new Background();
}//end constructor

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
    g.drawImage(launcher1.baldEagleImage, 350, 415, null);//paint the launcher
}//end paintComponent method

public void move() {
    launcher1.moveX();
    repaint();
}//end move method
}//end GamePanel class



public class Launcher {

public static int lxCoord;        //the launcher's x coordinate
public static final int lyCoord = 415;
public static int lRun = 0;           //the launcher's x change
public static BufferedImage baldEagleImage;

//Constructor
public Launcher() throws IOException {
    lxCoord = 350;
    baldEagleImage = ImageIO.read(new File("baldeagleimage.jpg"));
}

/**
 * The movement of the launcher in the x direction
 */
public void moveX() {
    lxCoord += lRun;
}//end moveX method


}//end Launcher class


public class Background extends JPanel {

BufferedImage background; 

public Background() throws IOException {
    background = ImageIO.read(new File("flagbackground.jpg"));
}//end constructor
}//end Background class

2 个答案:

答案 0 :(得分:1)

您创建了一个KeyListener但从未将其添加到任何内容中。此外,你的KeyListener可能不需要是一个JFrame,你可能有焦点问题所以我建议切换到游戏的key bindings

答案 1 :(得分:1)

addKeyListener( new aKeyListener() );
JFrame构造函数中的

应该这样做。您不应该使用aKeyListener扩展JFrame

另外,请考虑为KeyListener使用内部类,因为其他类可能不需要它。