Java AWT 在随机长度后停止监听输入

Java AWT Stops listening to input after random lengths of time

(抱歉,我无法解决问题的示例,我需要帮助!)

我正在创建自定义游戏引擎,并且遇到了一个问题,即当游戏 运行 - 游戏停止接收输入

我检查过,程序似乎在后台继续 运行。它似乎也不会因不同的机器而异 (我的主要设备是 Mac Book Pro 2011)

import java.awt.event.*;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;

public class Focus extends JFrame implements KeyListener {   
    private static final long serialVersionUID = 1L;
        char currKey = '[=11=]';
        public static void main(String[] args) throws IOException {
            SwingUtilities.invokeLater(new Runnable() {public void run() {new UIManager();}});
        }
        public Focus() throws IOException {     
            Container contentPane = getContentPane();
            contentPane.add(new DrawCanvas());
            addKeyListener(this);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setFocusable(true);
            setVisible(true);
        }    
        private class DrawCanvas extends JPanel {                   
            private static final long serialVersionUID = 1L;   
            public void paintComponent(Graphics pen) {
                //Drawloop
                if(currKey == 'k') {
                    //This is the code that randomly stops running
                    System.out.println("Yo");
                }
                try {
                    Thread.sleep(10);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                repaint();
            }
        }
        @Override
        public void keyTyped(KeyEvent e) {
            currKey = e.getKeyChar();
        }
        @Override
        public void keyPressed(KeyEvent e) {
            currKey = '[=11=]';
        }
        @Override
        public void keyReleased(KeyEvent e) {   
        }
}

代码在我看来是正确的(但是,它总是如此)唯一可能的触发点是 AWT 在 Main 中实例化,运行 在 UIManager 中实例化,移动代码驻留在播放器中,尽管我不知道如果您对 AWT 了解足够多,就知道是否会出现这种情况,并且重新定位备份中的代码会导致程序崩溃。任何帮助将不胜感激。

事实证明这是 Java 和 MacOS 密钥重复的问题-已在较新的 Java 版本中修复

修复更新java