重用计时器timertask Java

时间:2016-06-08 18:54:34

标签: java swing timer timertask

Soo使用扩展时间任务创建了一个计时器。

label_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                label_1.setVisible(false);
                label_2.setVisible(true);
                timer.purge();
                class MyTimeTask extends TimerTask
        {   
            public void run(){
            genReelNumbers();
            laa++;
           if(laa==50){
            timer.cancel();
            timer.purge();
                laa=0;
            label_1.setVisible(true);
            label_2.setVisible(false);}}}
                timer.purge();
                timer.schedule(new MyTimeTask(), 0, 50);}});

但是我已经取消了计时器的错误!正如你所看到我已经尝试使用purge(),soo它取消了“已取消”的计时器(不知道是否确实如此)。我想每次按下标签时都使用这个计时器!有什么想法吗?

3 个答案:

答案 0 :(得分:2)

首先,这看起来是一个Swing应用程序,如果是这样,你不应该使用java.util.Timer和java.util.TimerTask,因为Swing是单一的thread,上面的两个类创建一个或多个新线程来实现它们的动作,这意味着在这个线程上不会调用应该在Swing事件线程上调用的重要代码。这有可能导致有害的间歇性和难以调试的线程异常被抛出。而是使用javax.swing.Timer。然后要停止此计时器,只需在其上调用stop(),然后重新启动它,只需在其上调用start()即可。有关详情,请阅读:How To Use Swing Timers

例如,我不能100%确定你应该做什么代码,但它可能看起来像:

// warning: code not compile- nor run-tested
label_1.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent arg0) {
        label_1.setVisible(false);
        label_2.setVisible(true);

        // assuming a javax.swing.Timer field named timer
        if (timer != null && timer.isRunning()) {
            // if the timer is not null and it's running, stop it:
            timer.stop();
        }

        // TIMER_DELAY is an int constant that specifies the delay between "ticks"
        timer = new Timer(TIMER_DELAY, new ActionListener() {
            @Override  // this method will be called repeatedly, every TIMER_DELAY msecs
            public void actionPerformed(ActionEvent e) {
                genReelNumbers();
                laa++;
                if(laa==50){
                    timer.stop();
                    // timer.purge();
                    laa=0;
                    label_1.setVisible(true);
                    label_2.setVisible(false);
                }
            }
        });
        timer.start();
    }
});

答案 1 :(得分:0)

取消计时器后,除了创建新对象之外别无选择....

答案 2 :(得分:-1)

我按照@Hovercraft的建议改为javax.swing.Timer

结果是这样的:

//The variable "taxa" is the amount of times that i want it to do the task
    javax.swing.Timer time1 = new javax.swing.Timer(taxa, new ActionListener() {


            public void actionPerformed(ActionEvent e) {

                genReelNumbers();

            }
            });
//starts the timer
         time1.start();


//New timertask
            TimerTask tt = new TimerTask() {

            @Override
            public void run() {
//stops the timer
            time1.stop();
            label_2.setVisible(false);
            label_1.setVisible(true);
            verificarodas();
            }
            };
            Timer time = new Timer(true);

    // the 2000 is how long i want to do the task's
   //if i changed to 3000 it would take 3 seconds (remember it has to be a value on miliseconds) to do the 15 times, and soo on

            time.schedule(tt, 2000);