JavaFX图像幻灯片中的关键帧和持续时间

JavaFX图像幻灯片中的关键帧和持续时间,java,javafx,duration,timeline,keyframe,Java,Javafx,Duration,Timeline,Keyframe,我正在制作一个程序,其中有3个图像,它通过幻灯片放映,每个图像显示两秒钟。我的秒数与当前的秒数不同。但是我在声明关键帧的代码行中遇到了问题。我把所有的密码都放在下面了。任何关于我应该使用关键帧更改什么的建议,或者代码中其他任何内容。这是使用JavaFX /* * To change this license header, choose License Headers in Project Properties. * To change this template file, cho

我正在制作一个程序,其中有3个图像,它通过幻灯片放映,每个图像显示两秒钟。我的秒数与当前的秒数不同。但是我在声明关键帧的代码行中遇到了问题。我把所有的密码都放在下面了。任何关于我应该使用关键帧更改什么的建议,或者代码中其他任何内容。这是使用JavaFX

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package digitalpictureframe;

import java.io.File;
//import java.time.Duration;
import java.util.Arrays;
import javafx.util.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Zachary Murphy
 */
public class DigitalPictureFrame extends Application {


    @Override
    public void start(Stage primaryStage) {

        Image image1 = new Image("1.png");
        Image image2 = new Image("2.png");
        Image image3 = new Image("3.png");
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(

                new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
            new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image2)),  
            new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image3)),
            new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), null))
            );
        timeline.play();
        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

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

以下代码用于图像幻灯片。它循环浏览25幅图像,单击暂停,然后再次单击开始。我还使用了图像之间的淡入淡出动画。每个图像将保留2秒钟

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class lab12 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    // Global ImageView array variable.
    ImageView[] imgView = new ImageView[25];
    int imgIndex = 0;

    public void start(Stage stage) {

        Pane pane = new Pane();

        for (int i = 0; i < 25; i++) {
            imgView[i] = new ImageView(new Image("imagescards/" + i + ".jpg"));
            imgView[i].setFitWidth(600);
            imgView[i].setFitHeight(600);

        }

        pane.getChildren().add(imgView[imgIndex]);

        EventHandler<ActionEvent> eventHandler = e -> {
            if (imgIndex < 24) {
                // Adding Children
                pane.getChildren().remove(imgView[imgIndex]);
                imgIndex++;
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
            else if (imgIndex == 24) {
                imgIndex = 0;
                pane.getChildren().remove(imgView[24]);
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
        };

        // Timeline Animation
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(3000), eventHandler));

        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();

        pane.setOnMouseClicked(e -> {
            if (animation.getStatus() == Animation.Status.PAUSED) {
                animation.play();
            } else {
                animation.pause();
            }
        });

        Scene scene = new Scene(pane, 600, 600);

        stage.setScene(scene);
        stage.setTitle("Slide Show");
        stage.show();
    }
}
导入javafx.animation.animation;
导入javafx.animation.FadeTransition;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
导入javafx.scene.layout.Pane;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类lab12扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
//全局ImageView数组变量。
ImageView[]imgView=新的ImageView[25];
int imgIndex=0;
公众假期开始(阶段){
窗格=新窗格();
对于(int i=0;i<25;i++){
imgView[i]=新图像视图(新图像(“imagescards/“+i+”.jpg”);
imgView[i].setFitWidth(600);
imgView[i].设置高度(600);
}
pane.getChildren().add(imgView[imgIndex]);
EventHandler EventHandler=e->{
如果(imgIndex<24){
//添加子项
getChildren().remove(imgView[imgIndex]);
imgIndex++;
pane.getChildren().add(imgView[imgIndex]);
FadeTransition ft=新的FadeTransition(持续时间.毫秒(1000),imgView[imgIndex]);
ft.setFromValue(0);
ft.setToValue(1);
ft.play();
}
否则如果(imgIndex==24){
imgIndex=0;
pane.getChildren().remove(imgView[24]);
pane.getChildren().add(imgView[imgIndex]);
FadeTransition ft=新的FadeTransition(持续时间.毫秒(1000),imgView[imgIndex]);
ft.setFromValue(0);
ft.setToValue(1);
ft.play();
}
};
//时间轴动画
时间线动画=新时间线(新关键帧(Duration.millis(3000),eventHandler));
animation.setCycleCount(Timeline.unfinite);
动画。播放();
窗格。设置鼠标单击(e->{
if(animation.getStatus()==animation.Status.PAUSED){
动画。播放();
}否则{
暂停();
}
});
场景=新场景(窗格,600600);
舞台场景;
舞台。片名(“幻灯片放映”);
stage.show();
}
}

其他问题帖子根本帮不上我的忙……它说不适合关键帧的构造函数。如何修复?另一个问题没有回答我的关键帧有错误。我以前见过这个问题,并尝试过解决方案,因此我问这个问题!“不工作”是什么意思?相关问题的两个答案对我都适用(OSX10.9.5,Java运行时8u74)。如果遇到错误或异常行为,在问题中应包括编译错误或运行时堆栈跟踪,以及意外行为的描述和环境的详细描述(使用JDK版本等)。最好编辑问题并将格式化的堆栈跟踪放在其中,而不是放在注释中。