[英]How to use Timeline to perform a void method every x seconds on JavaFX?
[英]Use TimeLine to trigger a void method every certain seconds JavaFX
我試圖每隔幾秒鍾更新一次標簽,我嘗試使用普通的Timer,但是由於它在另一個線程中無法更改標簽,因此這是Timer:
public void setTimer(Timer timer, int seconds, String userName, String content, VBox tabContent,ArrayList<Integer> countTweetsArray, Label statusLabel) {
TabContent tabContentObj = new TabContent();
timer.schedule(new TimerTask() {
@Override
public void run() {
setTweet(userName, content);
//tabContentObj.createStatusScreen(tabContent, countTweetsArray, remainingTweets);
System.out.println(content+" after "+seconds);
System.out.println("countTweetsArray: "+countTweetsArray.get(0));
statusLabel.setText(countTweetsArray.get(0).toString());
countTweetsArray.set(0, (countTweetsArray.get(0)+1));
tabContentObj.timersMap.put(userName, timer);
}
}, (seconds*1000));
}
我讀到可以使用TimeLine定期更改標簽,但是我無法理解它如何工作鍵值和關鍵幀。有沒有一種方法可以觸發不帶任何動畫的void方法?
您可以使用帶有Duration
和事件處理程序的KeyFrame
構造函數:
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(seconds), e -> {
// code to execute here...
})
);
timeline.play();
更新:如果您需要一個按鈕來停止它,可以使用
Button button = new Button("Stop");
button.setOnAction(e -> timeline.stop());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.