TimeLINE javaFX 线程

TimeLINE javaFX thread

目前我有一个包含图像的列表,这些图像会使用时间轴显示一定时间。但是我希望为每张图片设置一个计数器,即如果每张图片的时间量为 30 秒,则应该有一个倒计时 运行 并显示在图片下方?有没有办法实现这种行为?

        Image i1 = new Image(getClass().getResourceAsStream("IMG_YOGA/01.png"),200,200,false,false);
        Image i2 = new Image(getClass().getResourceAsStream("IMG_YOGA/02.png"),200,200,false,false);
        Image i3 = new Image(getClass().getResourceAsStream("IMG_YOGA/03.png"),200,200,false,false);
        Image i4 = new Image(getClass().getResourceAsStream("IMG_YOGA/04.png"),200,200,false,false);
        Image i5 = new Image(getClass().getResourceAsStream("IMG_YOGA/05.png"),200,200,false,false);
        Image i6 = new Image(getClass().getResourceAsStream("IMG_YOGA/06.png"),200,200,false,false);
        Image i7 = new Image(getClass().getResourceAsStream("IMG_YOGA/07.png"),200,200,false,false);
        Image i8 = new Image(getClass().getResourceAsStream("IMG_YOGA/08.png"),200,200,false,false);

        Y_ImgView.setFitWidth(250);
        Y_ImgView.setFitHeight(300);
        
        final int NUM_FRAMES = 9;
        final int PAUSE_BETWEEN_FRAMES = 30;


        Timeline timeline = new Timeline();
        List<Image> images = List.of(i1,i2,i3,i4,i5,i6,i7,i8,i1);

        for (int i = 0; i < NUM_FRAMES; i++) {
            timeline.getKeyFrames().add(
                new KeyFrame(
                    javafx.util.Duration.seconds(i * PAUSE_BETWEEN_FRAMES), 
                    new KeyValue(Y_ImgView.imageProperty(), images.get(i))
                )
            );
        }

        timeline.setCycleCount(1);
        timeline.play();
        }

试试

IntegerProperty countdown = new SimpleIntegerProperty();
Label countdownLabel = new Label();
countdownLabel.textProperty().bind(countdown.asString());

// add label to layout

然后

    Timeline timeline = new Timeline();
    List<Image> images = List.of(i1,i2,i3,i4,i5,i6,i7,i8,i1);

    for (int i = 0; i < NUM_FRAMES; i++) {
        timeline.getKeyFrames().add(
            new KeyFrame(
                javafx.util.Duration.seconds(i * PAUSE_BETWEEN_FRAMES), 
                new KeyValue(Y_ImgView.imageProperty(), images.get(i)),
                new KeyValue(countdown, 0),
                new KeyValue(countdown, PAUSE_BETWEEN_FRAMES)
            )
        );
    }