2015-06-19 100 views
1

由於某種原因,我的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; 
}   
+1

對於Swing GUI的它通常應該是'javax.swing.Timer',而不是一個'java.uitl.Timer'。爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/)(Short,Self Contained ,正確的例子)。 –

回答

1

一個Timer只能安排一次,但調度本身是在一個固定的速度。只需撥打timer.scheduleAtFixedRate方法一次。如果您需要稍後啓動相同的計時器任務,則需要構建新的java.util.Timer

+0

不,我希望它在布爾型回答不正確時啓動定時器。什麼是錯,我不承諾? –

+0

@AlecStanton如果你想安排一個計時器任務重複運行,你需要使用'scheduleAtFixedRate'。您可以檢查TimerTask中的布爾值*。否則,您需要每次構建一個新的計時器並啓動它。 – hexafraction

1

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)

因爲你正試圖在這裏再次增加相同的任務:

timer.scheduleAtFixedRate(task, 0, duration); 
3

由於在標籤如前所述,你應該使用SwingTimer,而不是使用java.util.Timer

請看看這個樣本例如:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SwingTimerExample { 

    private String[] questions = { 
     "How are you?", 
     "How is your day?", 
     "Will you work tonight?" 
    }; 

    private JLabel questionLabel; 
    private ButtonGroup radioGroup; 
    private JRadioButton yesRadioButton; 
    private JRadioButton noRadioButton; 

    private int counter; 

    private static final int GAP = 5; 

    private Timer timer; 

    private ActionListener timerActions = new ActionListener() { 
     @Override 
     public void actionPerformed (ActionEvent ae) { 
      ++counter; 
      counter %= questions.length; 
      questionLabel.setText (questions [ counter ]); 
      if (counter == questions.length - 1) { 
       ((Timer) ae.getSource()).stop(); 
      } 
     } 
    }; 

    public SwingTimerExample() { 
     counter = 0; 
    } 

    private void displayGUI() { 
     JFrame frame = new JFrame ("Swing Timer Example"); 
     frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder (BorderFactory.createEmptyBorder (
      GAP, GAP, GAP, GAP)); 
     contentPane.setLayout (new BorderLayout (GAP, GAP)); 

     JPanel labelPanel = new JPanel(); 
     questionLabel = new JLabel ( 
          questions [ counter ], JLabel.CENTER); 
     labelPanel.add (questionLabel); 

     JPanel radioPanel = new JPanel(); 
     yesRadioButton = new JRadioButton ("YES"); 
     noRadioButton = new JRadioButton ("NO"); 
     radioGroup = new ButtonGroup(); 
     radioGroup.add (yesRadioButton); 
     radioGroup.add (noRadioButton); 
     radioPanel.add (yesRadioButton); 
     radioPanel.add (noRadioButton); 

     contentPane.add (labelPanel, BorderLayout.CENTER); 
     contentPane.add (radioPanel, BorderLayout.PAGE_END); 

     frame.setContentPane (contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform (true); 
     frame.setVisible (true); 

     timer = new Timer (5000, timerActions); 
     timer.start(); 
    } 

    public static void main (String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new SwingTimerExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater (runnable); 
    } 
} 
+0

只是打敗了我的評論! :) –

+1

@AndrewThompson:哈哈,只是試圖創建一個小程序的幫助。高興地知道,我來到前面,但從知識面前來說,你永遠是我的導師:-) –

+1

Aww .. shucks。繼續進行該計劃。我愛你的例子。 ;) –