由於某種原因,我的java定時器不能在我的一個程序中工作。每當我編譯我的代碼,它給我以下錯誤:Java定時器不工作
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
Task already scheduled or cancelled
at java.util.Timer.sched(Timer.java:401)
at java.util.Timer.scheduleAtFixedRate(Timer.java:328)
這是爲什麼? (注:我在的Java定時器新手)
//Timer Prerequisites
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
public void run()
{
System.out.println("We have waited one second.");
}
};
//Check to see if user has enetered anything
while(!answered)
{
timer.scheduleAtFixedRate(task, 0, duration);
afk = true;
incorrect += 1;
answered = true;
}
對於Swing GUI的它通常應該是'javax.swing.Timer',而不是一個'java.uitl.Timer'。爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/)(Short,Self Contained ,正確的例子)。 –