最近,作为初学者,我一直在使用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();
}
}
标签:slideshow,javafx,pagination,java
来源: https://codeday.me/bug/20191120/2043978.html