我有一个使用JavaFX的Timer,但它只计算秒数。我怎么能让它做分钟和秒。
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(
new KeyFrame(Duration.seconds(1),
//new {
new EventHandler<ActionEvent>() {
// KeyFrame event handler
public void handle(ActionEvent event) {
remaining--;
// update timerLabel
timeStamp.setText(
remaining.toString());
if (remaining <= 0) {
timeline.stop();
}
}
}));
timeline.playFromStart();
答案 0 :(得分:1)
Duration newValue=timeline.getCurrentTime();
int hTime = (int) newValue.toHours();
int minTime = (int) newValue.toMinutes();
int secTime= (int) newValue.toSeconds();
if(secTime/60>=1){ // this are to display later something like a clock 19:02:20
secTime%=60; //if you want just the time in minutes use only the toMinutes()
}
if(minTime/60>=1){
minTime%=60;
}
hTime =以小时为单位的时间
minTime =以分钟为单位的时间
secTime =以秒为单位的时间
通常时间轴与millis一起工作所以你必须要小心;)
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用提供良好转换功能的java.util.concurrent.TimeUnit
类
int min = 3;
int sec = 14;
double totalSec = TimeUnit.MINUTES.toSeconds(min) + sec;