2012-04-07 106 views
0

我有一個問題(我認爲)搖擺計時器。我編寫了一些運行正常的代碼,然後將它移到我的新計算機上,並迅速無法工作。我用這種方法編寫了一個GUI類(基於JFrame):Java搖擺計時器問題

public void Splash(){ 
    mainPanel.add(Empous.splash, BorderLayout.CENTER); 
    while(Empous.splash.GetCount() < 3){ 
       //System.out.println(Empous.splash.GetCount()); 
    } 
} 

該方法從另一個類中調用。 Empous.splash人是一個只運行飛濺動畫的JPanel類。它通過擺動計時器來完成。在飛濺類,聽者通過下面的代碼調整我的幀的內容:

private class TimerListener implements ActionListener{ 
    public void actionPerformed(ActionEvent evt) { 
     counter+=1; 
     if (counter==1){ 
      title2.setText("In Association With"); 
      title1.setText("El Pollo Diablo Productions"); 
     } 
     if (counter==2){ 
      remove(title2); 
      remove(title1); 
      repaint(); 
     } 
     if (counter==3){ 
      timer.stop(); 
     } 
    } 
} 

現在,如果在第一塊print語句被註釋掉了,我的計劃將在計時器停止後凍結。如果我取消註釋,程序將打印櫃檯的價值,然後繼續右下雨。我想把print語句拿出來,讓while循環運行而不做任何事情,但我不能這樣做,因爲這是當前的行爲。任何幫助讚賞。

+3

你應該創建一個[SSCCE](http://sscce.org),或者至少發佈更多的代碼。 – Jeffrey 2012-04-07 02:04:40

+2

請您瞭解[Java命名約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367),並在編碼時遵守它們:-) – 2012-04-07 04:12:18

+0

請參閱[Loop doesn如果沒有打印語句,就不會看到更改的值](https://stackoverflow.com/questions/25425130/loop-doesnt-see-changed-value-without-a-print-statement)一個區別。 – Boann 2015-01-16 23:34:16

回答

4

你看看被打破Swing的線程規則,這個while循環:

while(Empous.splash.GetCount() < 3){ 
    //System.out.println(Empous.splash.GetCount()); 
} 

爲什麼你連這個,因爲你已經有了一個Swing定時器將處理這樣的事情,而不freesing事件線程?

雖然,更具體的建議是,發佈sscce

+0

當我遇到這個時,我想我已經超過了我的Java知識。我仍然不太瞭解一些線程概念,所以我會再讀一些 - 感謝提示。 – Angineer 2012-04-07 04:10:18