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

标签 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

最佳答案

您创建了一个 KeyListener 但从未将其添加到任何内容中。另外,您的 KeyListener 可能不需要是 JFrame,并且您可能会遇到焦点问题,因此我建议切换到 key bindings为了一场比赛。

关于java - 我在添加关键监听器时犯了什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229853/

相关文章:

java - 如何根据双击 JTable 的位置生成新面板?

java - JLabel到JPanel,拖放

java - 如何使用Google Snapshot Api在线存储应用程序数据?

java - 无法运行 Blackberry Advanced UI 示例

java - 图像导出 DayPilot 调度程序

java - 我的 JTextArea 继续无限,尽管添加了 LineWrap 等

java - 动态更改 JTable 字体大小

java - 需要简单的方法来修改 JTable 使用的 toString()

java - Android:添加进度条?

Java 在 JPanel 上绘图,而 JPanel 在 JFrame 上绘图