2015-06-19 59 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。继续进行该计划。我爱你的例子。 ;) –