简体   繁体   English

KeyListener无法正常工作

[英]KeyListener not working

public class KL implements KeyListener {

 public static void main(String[] args) {
        final JPopupMenu popup = new JPopupMenu();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

    }

@Override
public void keyPressed(KeyEvent arg0) {
    System.out.println(arg0.getKeyChar());
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println(e.getKeyChar());

}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println(e.getKeyChar());

}
}

That's my class, it's probably something really stupid on my part, but my KeyListener here is not working. 这是我的课程,这可能是我非常愚蠢的事情,但我的KeyListener不起作用。 Nothing comes up on the console. 控制台上没有任何内容。

Let's start with the fact that you're not attached the listener to anything, then move on to the fact that you really should be using Key Bindings 让我们从你没有将监听器附加到任何东西的事实开始,然后继续讨论你真的应该使用Key Bindings的事实

And with example 并举例说明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTableEditing {

    public static void main(String[] args) {
        new TestTableEditing();
    }

    public TestTableEditing() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel key;
        private int counter = 0;

        public TestPane() {
            key = new JLabel("...");
            add(key);
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A.pressed");
            am.put("A.pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("A was pressed");
                    key.setText("A was pressed " + (++counter));
                }
            });
        }

    }

}

I know this is an old post but I wanted to put this online so someone like myself can find it.... 我知道这是一个老帖子,但我想把它放在网上,所以像我这样的人可以找到它....

I worked on this problem for hours before figuring it out. 在弄清楚问题之前,我已经解决了这个问题几个小时。 Make sure that your Component has focus. 确保您的Component具有焦点。 For example I have all of my activity going on in a custom JPanel named SpaceShipPanel: 例如,我将所有活动都放在名为SpaceShipPanel的自定义JPanel中:

class SpaceShipPanel
{
    //instance variables
    //Now my constructor
    SpaceShipPanel(){
        //bla bla blah
        setFocusable(true);//THIS LINE IS WHAT SAVED ME!!
    }
}

From what I hear, keyBindings are the best route but the class I'm taking didn't cover this topic. 根据我的意见,keyBindings是最好的路线,但我所参加的课程并没有涵盖这个主题。 Hopefully this will save someone hours of beating their heads against the wall. 希望这可以节省一些时间在墙上敲打他们的头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM