【问题标题】:Auto Slideshow in JavaFX PaginationJavaFX 分页中的自动幻灯片放映
【发布时间】:2015-06-30 21:39:51
【问题描述】:

作为初学者,我最近一直在使用 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();
}

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

【问题讨论】:

    标签: java javafx pagination slideshow


    【解决方案1】:

    这很容易。您所要做的就是创建一个每 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 2012-10-13
      • 2015-04-21
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多