KeyAdapter 没有响应 ~ Java

发布于 2024-10-18 01:41:15 字数 1350 浏览 1 评论 0原文

我正在创建一个简单的突破游戏。但是,KeyAdapter 未接收输入。该代码对我来说看起来不错,但也许我错过了一些更基本的东西?

public DatGamePanel(BustOut bo, long framerate) {

    setBackground(Color.black);
    setPreferredSize( new Dimension(GAME_WIDTH,GAME_HEIGHT));
    setFocusable(true);

    font = new Font("Sans Serif", Font.BOLD, 24);
    fm = this.getFontMetrics(font);

    this.bo = bo;
    period = 1000/framerate;
    bat = new Bat("bat.png",GAME_WIDTH,GAME_HEIGHT-32,2);

    //Get keyboard input :D
    addKeyListener( new KeyAdapter() {
        public void keyPressed(KeyEvent ke) {
            handleInputPressed(ke);
        }

        public void keyReleased(KeyEvent ke) {
            handleInputReleased(ke);
        }
    });
}

public void handleInputPressed(KeyEvent ke) {
    int a = ke.getKeyCode();
    switch(a) {
        case KeyEvent.VK_LEFT:
        bat.keyHandle(0);
        test = 1;
        break;

        case KeyEvent.VK_RIGHT:
        bat.keyHandle(2);
        break;
    }
}

public void handleInputReleased(KeyEvent ke) {
    System.out.println("Key Pressed");
    int a = ke.getKeyCode();
    switch(a) {
        case KeyEvent.VK_LEFT:
        bat.keyHandle(1);
        test = 0;
        break;

        case KeyEvent.VK_RIGHT:
        bat.keyHandle(3);
        break;
    }       
}

这些都是基本的输入句柄。当我按下向左箭头时,测试变量不会改变。这里出了什么问题...

I'm creating a simple breakout game. However, the KeyAdapter isn't receiving the input. The code looks fine to me but maybe I'm missing something more basic?

public DatGamePanel(BustOut bo, long framerate) {

    setBackground(Color.black);
    setPreferredSize( new Dimension(GAME_WIDTH,GAME_HEIGHT));
    setFocusable(true);

    font = new Font("Sans Serif", Font.BOLD, 24);
    fm = this.getFontMetrics(font);

    this.bo = bo;
    period = 1000/framerate;
    bat = new Bat("bat.png",GAME_WIDTH,GAME_HEIGHT-32,2);

    //Get keyboard input :D
    addKeyListener( new KeyAdapter() {
        public void keyPressed(KeyEvent ke) {
            handleInputPressed(ke);
        }

        public void keyReleased(KeyEvent ke) {
            handleInputReleased(ke);
        }
    });
}

public void handleInputPressed(KeyEvent ke) {
    int a = ke.getKeyCode();
    switch(a) {
        case KeyEvent.VK_LEFT:
        bat.keyHandle(0);
        test = 1;
        break;

        case KeyEvent.VK_RIGHT:
        bat.keyHandle(2);
        break;
    }
}

public void handleInputReleased(KeyEvent ke) {
    System.out.println("Key Pressed");
    int a = ke.getKeyCode();
    switch(a) {
        case KeyEvent.VK_LEFT:
        bat.keyHandle(1);
        test = 0;
        break;

        case KeyEvent.VK_RIGHT:
        bat.keyHandle(3);
        break;
    }       
}

These are all the basic input handles. The test variable doesn't change when I push the Left arrow. What's wrong here...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2024-10-25 01:41:15

如果您只是监听几个按键,并且执行监听的组件可能没有焦点,那么使用按键绑定比使用 KeyListener 要好得多。请查看此处如何使用按键绑定

如果此建议不适用似乎没有帮助,请考虑创建并发布一个 SSCCE (请单击链接),这是一个可编译、可运行的小型程序,用于演示你为解决这个问题所做的最大努力。然后我们可以检查您的代码、运行它、修改它,并最好能够帮助您修复它。

If you're just listening for a few keys and your component doing the listening may not have the focus, you're far better of using key bindings than a KeyListener. Please look here How to use Key Bindings

If this recommendation doesn't seem to help, consider creating and posting an SSCCE (please click on the link), a small compilable, runnable program that demonstrates your best attempt at solving this. Then we can inspect your code, run it, modify it and best be able to help you fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文