2014-10-28 85 views
1

倒計時不起作用。我通過一個按鈕觸發它。JAVAFX,倒計時不起作用

public void startCountDown() { 
    timer.schedule(new TimerTask() { 
    @Override 
     public void run() { 
     Platform.runLater(new Runnable() { 
      public void run() { 
       countDown--; 
       countDownText.setText("Time left:" + countDown); 

       if (countDown < 0) 
        timer.cancel(); 
      } 
     }); 
    } 
    }, 1000); //Every 1 second 
} 

倒計時變量被設置爲60,所以倒計時開始於60

編輯:倒計時卡住在59秒時,倒計時一個int。沒有錯誤。並且countDownText被聲明爲文本。

@FXML 
private Text countDownText; 
+0

你能否詳細說明上「不起作用」?它會產生錯誤嗎?或者它只是不做你想要的?編輯你的問題也許會有幫助,以顯示(至少)'countDown'和'countDownText'的聲明。 – 2014-10-28 18:34:57

回答

4

有超過1個定時器類,但我猜你用java.util.Timer中

檢查的API顯示了使用這種方法:

public void schedule(TimerTask task, 
      long delay) 

Schedules the specified task for execution after the specified delay. 

Parameters: 
    task - task to be scheduled. 
    delay - delay in milliseconds before task is to be executed. 
Throws: 
    IllegalArgumentException - if delay is negative, or delay + System.currentTimeMillis() is negative. 
    IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. 
    NullPointerException - if task is null 

所以,你的程序等待1秒然後執行run()方法,但不會重複。爲了使重複,你需要使用這個方法:

public void schedule(TimerTask task, 
      long delay, 
      long period) 

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals separated by the specified period. 

In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate). 

Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down. 

Parameters: 
    task - task to be scheduled. 
    delay - delay in milliseconds before task is to be executed. 
    period - time in milliseconds between successive task executions. 
Throws: 
    IllegalArgumentException - if delay < 0, or delay + System.currentTimeMillis() < 0, or period <= 0 
    IllegalStateException - if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated. 
    NullPointerException - if task is null 

可以查看這裏的完整文檔:http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#schedule%28java.util.TimerTask,%20long%29

這裏是什麼,我認爲是正確的代碼:

public void startCountDown() { 
    timer.schedule(new TimerTask() { 
    @Override 
     public void run() { 
     Platform.runLater(new Runnable() { 
      public void run() { 
       countDown--; 
       countDownText.setText("Time left:" + countDown); 

       if (countDown < 0) 
        timer.cancel(); 
      } 
     }); 
    } 
    }, 1000, 1000); //Every 1 second 
} 
+0

+1謝謝,作品像一個魅力! :) – Sh0ck 2014-10-28 19:21:27