English 中文(简体)
Java: 在已经可见的 JFrame 上添加一个密钥收听器
原标题:Java: Adding a keyListener to an already visible JFrame

再说一遍,我和钥匙听众和JFrame还有另一个问题。我想在JFrame明显可见的时候,给JFrame添加一个钥匙听众。我的问题是,当我尝试这个时,钥匙听众没有回应。这是我的代码:

public JFrame frame = new JFrame("JFrame Test");

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

public JFrameTest() {
    frame.setSize(100, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    frame.add(new space());
    frame.validate();
    frame.repaint();
}

@SuppressWarnings("serial")
public class space extends JPanel {
    public space() {
        setFocusable(true);
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                System.out.println("Mouse Pressed!");
            }

            public void mouseReleased(MouseEvent e) {
                System.out.println("Mouse Released!");
            }
        });
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                System.out.println("Key Pressed!");
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("Key Released!");
            }
        });
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(0, 0, frame.getWidth() / 2, frame.getHeight() / 2);
        repaint();
    }
}

我推迟了添加关键听力器以代表用户按下按钮的时间, 而该按钮反过来会激活关键听力器。 我知道睡眠不是问题所在, 因为在我的大型工程中, 我也有同样的问题, 但是在等待 x 时间后按下按钮来加载游戏空间。 我不知道我是否在重新油漆和验证正确, 但似乎没有人有这个问题。 任何帮助都会感激不尽。 谢谢!!

最佳回答

这个问题可能与您使用<的焦点子系统 时的错误有关。

,大卫表示,尽管小组的设立是正确的,

... 面板不是要求焦点。 请使用我的面板. requestFocus () ;

此外,Keyliister s 上的爪哇教程提到:

当用户按键或发布键盘键时,关键事件由键盘焦点 的 < 坚固 > 组件启动。

(着重号后加)。

所以,你不应该使用 add( 新空间 ()) ,而是应该:

space s = new space();
add(s);
s.requestFocus();

这应该能解决你的问题

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...