简体   繁体   English

Java计时器不起作用

[英]Java Timer Not Working

For some reason my java timer is not working in one of my programs. 由于某种原因,我的Java计时器无法在我的程序之一中运行。 Whenever I compile my code, it gives me the following error: 每当我编译代码时,都会出现以下错误:

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)

Why is this? 为什么是这样? ( Note: I am a novice at Java timers) 注意:我是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;
}          

Since as noted in the tag, you should be using Swing Timer , instead of using the java.util.Timer . 由于正如标记中所述,您应该使用Swing Timer而不是java.util.Timer

Please have a look at this sample example: 请看以下示例示例:

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 );
    }
}

A Timer can only be scheduled once, but the scheduling itself is at a fixed rate. 一个Timer只能安排一次,但是安排本身是固定的。 Simply call the timer.scheduleAtFixedRate method once. 只需调用一次timer.scheduleAtFixedRate方法。 If you need to start the same timer task later, you will need to construct a new java.util.Timer . 如果以后需要启动同一计时器任务,则需要构造一个新的java.util.Timer

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) 线程“ AWT-EventQueue-0”中的异常java.lang.IllegalStateException:已在java.util.Timer.scheduleAtFixedRate(Timer.java:328)的java.util.Timer.sched(Timer.java:401)安排或取消了任务)

Because you are trying to add the same task again here: 因为您尝试在此处再次添加相同的任务:

timer.scheduleAtFixedRate(task, 0, duration);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM