2012-06-14 29 views
1
public void paint (Graphics g) 
{ 
    bufferGraphics.setColor (Color.black); 
    bufferGraphics.clearRect (0, 0, dim.width, dim.width); 
    while (input != KeyEvent.VK_SPACE) 
    { 
     bufferGraphics.fillRect (0, 0, dim.width, dim.height); 
    } 

    bufferGraphics.drawImage (track, 0, 0, dim.width, dim.height, this); 
    bufferGraphics.setFont (new Font ("Calibri", Font.PLAIN, 25)); 
    bufferGraphics.drawString ("Level: " + level, 30, 30); 
    bufferGraphics.drawImage (car, 620, myCarY, 70, 120, this); 
    bufferGraphics.drawImage (opponent, 415, oppCarY, 70, 120, this); 
    move(); 

這是現在的代碼。執行時,我會得到一個甚至無法關閉的凍結空白窗口。重要聽衆不能使用

+1

您是否打算將「false」的值分配給「run」? – Glenn

+0

是的,我是一個初學者,對不起 –

+0

只需嘗試:while(!run){...}語句「run = false」將值「false」賦值給「run」。 – Glenn

回答

1

你的問題在你的if語句中。

if(run = false) 

將永遠不會執行,因爲賦值返回被賦值的值(例如false)。您需要將=更改爲==

你也可以改變你的無限for循環while循環,像

while(input != KeyEvent.VK_SPACE) { 
} 

另外,還要確保你的KeyListener被添加到您的類(在構造函數)

addKeyListener(new MyKeyListener())

我剛剛測試了代碼,它工作。

+0

如果這樣做,然後程序卡在那無盡的循環,並將永遠不會中斷當空格鍵被按下 –

+0

是的我試過,沒有運氣,任何其他的建議? –

+0

我是初學者,不熟悉Frame/JFrame。 KeyListener只是我的代碼底部的一個單獨的類 –