Displaying changing values in JavaFx Label

后端 未结 4 1024
一生所求
一生所求 2020-11-30 05:10

In JavaFX, how can I display values which continuously change with time using \"label\" ?

4条回答
  •  没有蜡笔的小新
    2020-11-30 05:29

    Excellent responses, thank you jewelsea for your input it helped me a lot.

    I updated the DigitalClock posted previously in a leaner format using Java 8. Using the additions of Java 8 like Date API and of course the lambdas.

     import javafx.animation.Animation;
     import javafx.animation.KeyFrame;
     import javafx.animation.Timeline;
     import javafx.scene.control.Label;
     import javafx.util.Duration;
    
     import java.time.LocalTime;
     import java.time.format.DateTimeFormatter;
    
     public class DigitalClock extends Label
     {
        private static DateTimeFormatter SHORT_TIME_FORMATTER =       DateTimeFormatter.ofPattern("HH:mm:ss");
    
        public DigitalClock()
        {
            bindToTime();
        }
    
        private void bindToTime() {
            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0),
                                                          event -> setText(LocalTime.now().format(SHORT_TIME_FORMATTER))),
                                             new KeyFrame(Duration.seconds(1)));
    
            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play();
        }
    }
    

提交回复
热议问题