我正在使用JavaFx的动画框架来构建一个计时器,该计时器将从25分钟开始倒计时。
现在,我的代码从25分钟开始倒计时,但没有每秒更新或设置Label动画。到目前为止,这是我尝试过的:
这是事件处理程序,在单击按钮时开始倒计时
public class CountdownActionEvent implements EventHandler<ActionEvent> {
private Timer defaultTime;
private Label timer;
private Timeline animation;
private int minutes;
private int seconds;
public CountdownActionEvent(Timer timer, Label label) {
this.defaultTime = timer;
this.timer = label;
}
private void CountDown() {
for (this.minutes = 25; minutes >= 0; minutes--) {
for (this.seconds = 59; seconds >= 0; seconds--) {
this.defaultTime.setMinutes(minutes);
this.defaultTime.setSeconds(seconds);
timer.setText(defaultTime.toString());
}
}
}
@Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
animation = new Timeline(new KeyFrame(Duration.seconds(1), e -> CountDown()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
}
这也是分配给“ defaultTime”对象的Timer类:
public class Timer {
private String minutes;
private String seconds;
public Timer() {
this.minutes = "25";
this.seconds = "00";
}
public String getMinutes() {
return this.minutes;
}
public String getSeconds() {
return this.seconds;
}
public void setMinutes(int min) {
if (min < 10) {
this.minutes = "0" + min;
} else {
this.minutes = min + "";
}
}
public void setSeconds(int sec) {
if (sec < 10) {
this.seconds = "0" + sec;
} else {
this.seconds = sec + "";
}
}
public String toString() {
return this.minutes + ":" + this.seconds;
}
}
当我运行该程序时,按下按钮后,标签“计时器”立即更新为0。我试图寻找可能的解决方案,但似乎无法解决此问题。任何帮助表示赞赏。谢谢!
相关分类