首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyListener不听

KeyListener不听
EN

Stack Overflow用户
提问于 2015-09-06 05:50:01
回答 2查看 632关注 0票数 0

我有以下代码。当我按向右箭头时,需要打印一条消息。但是当我按下VK_RIGHT时,键监听器不会触发。它不会打印它应该打印的消息。你知道为什么吗?

代码语言:javascript
复制
public class AutoClicker implements KeyListener{

       public static int rate = 0;
       static boolean  keep = true;

   public static void main(String[] args) {
       AutoClicker clicker = new AutoClicker();
      JFrame frame = new JFrame();
      frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(new Dimension(200,300));
       frame.setVisible(true);

       frame.setFocusable(true);
       frame.requestFocus();
   }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
;
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            keep = false;
            System.out.println(keep);

            //System.out.println("Right key typed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key typed");
        }

        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyReleased(KeyEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-09-06 05:57:21

首先,您不应该将KeyListeners用于Swing应用程序。请看一下How to Use Key Bindings。现在来看看你的主要问题:

您没有在框架中添加KeyListener,这就是它不起作用的原因。因此,添加frame.addKeyListener(this); (或者理想情况下,添加到您想要拥有键侦听器的面板中),它就可以工作了--但是您不能在静态上下文中执行此操作,因此我建议在构造函数中创建框架。(并且在main方法中仅调用new AutoClicker() )。你也可以在另一个答案中使用解决方案,但我更喜欢这样:P

代码语言:javascript
复制
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class AutoClicker implements KeyListener {

    public static int rate = 0;
    static boolean keep = true;

    public AutoClicker() {

        JFrame frame = new JFrame();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(200, 300));
        frame.setFocusable(true);
        frame.addKeyListener(this);
        frame.requestFocus();
        frame.setVisible(true);

    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AutoClicker();
            }
        });

    }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    };

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            keep = false;
            System.out.println(keep);

            System.out.println("Right key typed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key typed");
        }

        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // throw new UnsupportedOperationException("Not supported yet."); //To
        // change body of generated methods, choose Tools | Templates.
    }

}
票数 2
EN

Stack Overflow用户

发布于 2015-09-06 06:14:03

您必须将keylistener添加到帧中。只需根据以下代码更新你的main方法。

代码语言:javascript
复制
   AutoClicker clicker = new AutoClicker();
   JFrame frame = new JFrame();
   frame.addKeyListener(clicker);
   frame.setLocationRelativeTo(null);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(new Dimension(200,300));
   frame.setVisible(true);

   frame.setFocusable(true);
   frame.requestFocus();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32418013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档