【问题标题】:What mistake am I making in adding my key listener?我在添加我的关键听众时犯了什么错误?
【发布时间】:2014-04-22 20:46:32
【问题描述】:

我正在制作的游戏的启动器对象(由秃鹰图片表示)没有响应关键事件。我知道我声明监听器的方式有问题,但我不确定我做错了什么。

这是我的代码:

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

【问题讨论】:

标签: java swing jpanel paintcomponent keylistener


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    addKeyListener( new aKeyListener() );
    

    JFrame 构造函数中应该这样做。你不应该用JFrame扩展你的aKeyListener

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

    【讨论】:

    • 感谢您的回复。我仍然没有得到任何回应,所以我想我搞砸了实施你的建议。我更新了我的代码,你有没有机会回顾它并指导我哪里出错了?我不想要解决方案,只是伸出援助之手。我是 OOP 的新手,也是一般的编程新手。
    • @Ben - 我测试了您的代码,并且您的 KeyListener 实现很好(尽管我建议扩展 KeyAdapter 而不是实现 KeyListener 到您的内部类以摆脱未使用的方法,比如keyReleased)。尝试在您的 keyPressed 中输入System.out.print("something");,您会注意到,只要您按下按钮,它就会输入该内容。问题出在其他地方。
    • 谢谢,我意识到我的paintComponent 方法只是在每次调用repaint() 时将启动器重新绘制到初始位置。我修复了它,现在当我点击“a”按钮时它会移动。感谢您的帮助!
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2023-01-13
    • 2023-03-03
    • 1970-01-01
    • 2017-01-18
    相关资源
    最近更新 更多