(對(duì)不起,我無法解決這個(gè)問題,我需要幫助?。┪艺趧?chuàng)建一個(gè)自定義游戲引擎,并遇到了一個(gè)問題,即在游戲運(yùn)行時(shí) - 游戲停止接受輸入我檢查過,程序似乎繼續(xù)在后臺(tái)運(yùn)行。它似乎也不會(huì)因不同的機(jī)器而有所不同(我的主要設(shè)備是 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 = '\0'; 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 = '\0'; } @Override public void keyReleased(KeyEvent e) { }}代碼在我看來是正確的(但是,它總是如此)并且唯一可能的觸發(fā)點(diǎn)是 AWT 在 Main 中實(shí)例化,在 UIManager 中運(yùn)行并且移動(dòng)代碼駐留在播放器中,盡管我對(duì) AWT 的了解不夠了解是否情況就是如此,重新定位備份中的代碼會(huì)導(dǎo)致程序崩潰。任何幫助將不勝感激。
1 回答

互換的青春
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
事實(shí)證明這是 Java 和 MacOS 鍵重復(fù)的問題——已在較新的 Java 版本中修復(fù)
修復(fù)更新java
添加回答
舉報(bào)
0/150
提交
取消