簡體   English   中英

JavaFX時間軸包括分鍾和秒

[英]JavaFX Timeline Include Minutes and Seconds

我有一個使用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();
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配合使用,因此您必須小心;)

您使用getTime方法

http://docs.oracle.com/javafx/2/api/javafx/animation/KeyFrame.html

我認為這對您有幫助。

您可以使用java.util.concurrent.TimeUnit類,該類提供了不錯的轉換工具

int min = 3;
int sec = 14;
double totalSec = TimeUnit.MINUTES.toSeconds(min) + sec;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM