2016-06-08 27 views
0

Soo使用擴展時間任務創建了一個定時器。重用定時器timertask Java

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(),所以它取消了「取消」的定時器(不知道它是否會產生任何意義)。每次我按下標籤時,我都想使用此計時器!有任何想法嗎?

回答

-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); 
+0

您現在似乎在TimerTask中使用Swing Timer和java.util.Timer,如果是這樣,我不建議您這樣做,因爲我的答案中列出了相同的原因。如果您想要更全面的答案,請告知更多關於您正在嘗試使用這些定時器的信息。你試圖引出什麼樣的行爲? –

0

取消你沒有其他選擇,而不是創建一個新對象定時器後....

+0

洙如何我還可以使用定時器,但讓他「停」? –

+0

他不應該使用java.util.Timer,因爲這是一個Swing應用程序。 –

2

首先,這看起來是一個Swing應用程序,如果是這樣,你不應使用java.util.Timer和java.util.TimerTask,因爲Swing是單線程的,上面的兩個類創建一個或多個新線程來實現他們的動作,這意味着應該在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(); 
    } 
}); 
+0

好吧,我遵循你的建議,並開始使用搖擺計時器!謝謝! –