Java 计时器不工作 [英] Java Timer Not Working

查看:31
本文介绍了Java 计时器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的 Java 计时器在我的一个程序中不起作用.每当我编译我的代码时,它都会给我以下错误:

For some reason my java timer is not working in one of my programs. 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)

这是为什么?(注意:我是 Java 计时器的新手)

Why is this? (Note: I am a novice at Java timers)

//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 定时器,而不是使用 java.util.Timer.

Since as noted in the tag, you should be using Swing Timer, instead of using the 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 );
    }
}

这篇关于Java 计时器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆