KeyListener不会监听

时间:2015-09-05 21:50:01

标签: java swing keylistener

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

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.
    }
}

2 个答案:

答案 0 :(得分:2)

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

您没有在框架中添加KeyListener,这就是为什么它不起作用。所以添加frame.addKeyListener(this);(或者理想情况下添加到你想拥有密钥监听器的面板)它会起作用 - 但是你不能在静态上下文中创建它,所以我建议在构造函数中创建框架。 (并在主方法中仅调用new AutoClicker() )。您也可以在另一个答案中使用该解决方案,但我只是喜欢这样:P

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.
    }

}

答案 1 :(得分:1)

您必须在框架中添加keylistener。只需根据以下代码更新您的main方法即可。

   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();