我正在尝试使用java fx进行简单的幻灯片放映。我正在实现允许我按下按钮后显示一个图像的方法,直到列表结束。 现在我有一个像这样的代码......它有效,但它显示图像太快我不知道为什么。也许有人可以帮助我告诉我如何减慢这个时间线变量或timer.thanks提前:
package pl.gallery.controller;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import pl.gallery.model.ImageParser;
public class MainPaneController implements Initializable {
@FXML
private BorderPane borderPane;
@FXML
private Button nextButton;
@FXML
private MenuBar menuBar;
@FXML
private AnchorPane anchorPaneTop;
@FXML
private HBox hBox;
@FXML
private Button previousButton;
@FXML
private MenuItem openFolder;
@FXML
private AnchorPane anchorPaneCenter;
@FXML
private ImageView imageView;
@FXML
private Button slideShowButton;
@FXML
private Menu menu;
private Image image;
private ImageParser parser;
private ObservableList<Image> imagesList;
private int indexPrev = 0;
private int indexNext = 0;
private Timeline timeline;
private AnimationTimer timer;
//variable for storing actual frame
private Integer i=0;
@Override
public void initialize(URL location, ResourceBundle resources)
throws IndexOutOfBoundsException {
parser = new ImageParser();
imagesList = FXCollections.observableArrayList();
DirectoryChooser dc = new DirectoryChooser();
openFolder.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
/*
* parser = new ImageParser(); imagesList = new
* ArrayList<Image>(); DirectoryChooser dc = new
* DirectoryChooser(); File dir = dc.showDialog(new Stage());
* parser.createFileList(dir);
*
*
* imagesList.add(new Image(parser.getFilesList().get(0)
* .toURI().toString())); imageView.setImage(imagesList.get(0));
*/
File dir = dc.showDialog(new Stage());
imagesList = parser.createImagesListFromFileList(dir);
imageView.setImage(imagesList.get(0));
}
});
slideShowButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
Duration duration = new Duration(1000);
timeline.setDelay(duration);
timer = new AnimationTimer() {
@Override
public void handle(long l) {
imageView.setImage(imagesList.get(i));
i++;
}
};
timeline.play();
timer.start();
}
});
}
}