Vha*_*lad 1 java label timeline javafx timer
我试图每隔几秒钟更新一次标签,我尝试使用普通的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));
}
Run Code Online (Sandbox Code Playgroud)
我读到可以使用TimeLine定期更改标签,但是我无法理解它如何工作键值和关键帧。有没有一种方法可以触发不包含任何动画的void方法?
您可以使用带有和事件处理程序的KeyFrame构造Duration函数:
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(seconds), e -> {
// code to execute here...
})
);
timeline.play();
Run Code Online (Sandbox Code Playgroud)
更新:如果您需要一个按钮来停止它,可以使用
Button button = new Button("Stop");
button.setOnAction(e -> timeline.stop());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5020 次 |
| 最近记录: |