JavaFX 分页中的自动幻灯片放映

Auto Slideshow in JavaFX Pagination

作为初学者,我最近一直在使用 JavaFx,给我留下了深刻的印象。目前我一直在尝试将分页幻灯片设置为自动 每 5 秒向前移动幻灯片(并在到达最后一张幻灯片时返回第一张幻灯片以继续)。任何人都可以在这里引导我朝着正确的方向前进吗?

    @FXML
public void slideshow(ActionEvent event) {
    // TODO Auto-generated method stub
    String[] photos = { "housestark.jpg", "housefrey.jpg", "housebar.jpg",
            "HouseBolton.jpg", "housegreyjoy.jpg", "houseaaryn.jpg",
            "houselannis.jpg", "housemart.jpg", "housereed.jpg",
            "housetully.jpg", "housetyrel.jpg", };
    Pagination p = new Pagination(photos.length);
    p.setPageFactory((Integer pageIndex) -> {
        return new ImageView(getClass().getResource(photos[pageIndex])
                .toExternalForm());
    });

    Stage stage = new Stage();
    stage.setScene(new Scene(p));
    stage.setX(1250);
    stage.setY(10);
    stage.setTitle("Slideshow");
    stage.setResizable(false);
    stage.show();
}

到目前为止,这是我的代码!如果有人能提供任何帮助,我将不胜感激?

这很容易。您所要做的就是创建一个每 5 秒运行一次的计时器,并在它运行时移动页面索引。

public class SO extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Pagination p = new Pagination(10);

        Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
            int pos = (p.getCurrentPageIndex()+1) % p.getPageCount();
            p.setCurrentPageIndex(pos);
        }));
        fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
        fiveSecondsWonder.play();

        stage.setScene(new Scene(p));
        stage.show();
    }
}

五秒奇迹出自这里:JavaFX periodic background task