1
我有一個用於擴展動畫的JPanel的類中的Timer,並且ActionListener偵聽它並使actionPerformed運行,它會在需要時重繪並停止計時器。但啓動定時器animatePanel的方法會在定時器運行時繼續執行,這是我不想要的。我希望它等到定時器停止返回。如何在執行代碼之前等待計時器停止?
定時器是在類的構造函數初始化這樣的:
timer = new Timer(5, taskPerformer);
這是它做什麼。我有話通話animatePanel():
private ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
...
if (some conditions){
...
timer.stop();
...
return;
}
...
}
};
private void animatePanel() {
...
timer.start();
System.out.println("Timer stopped."); //always executes before the timer has stopped :(
//then returns and lets the rest of my program run while the timer is still going, which is BAD
}
定時器工作正常,只是在某些情況下,animatePanel()也將很快恢復,讓我的程序運行的剩餘部分,造成的問題。
你能否介紹計時器變量的聲明? – 2013-04-22 01:56:07
爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-22 01:56:27
如果你從EDT的環境中啓動計時器,那麼這將炸燬在你的臉上,只是說... – MadProgrammer 2013-04-22 01:57:22