【发布时间】:2016-03-11 20:40:34
【问题描述】:
我正在制作一个程序,其中有 3 张图像,它通过幻灯片放映,每张图像显示两秒钟。我的秒数与当前的秒数不同。但是我在声明 KeyFrame 的代码行中遇到了问题。我把我所有的代码都放在下面。关于我应该在我的代码中使用关键帧或其他任何内容进行更改的任何建议。这是使用 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
【问题讨论】:
-
其他问题帖子对我没有帮助...
-
它说不适合关键帧的构造函数。我该如何解决?
-
@jewelsea 另一个问题没有回答我的关键帧有错误。我之前看到过这个问题并尝试了解决方案,因此我问这个问题!
-
“不起作用”是什么意思?相关问题的两个答案都对我有用(OS X 10.9.5,Java Runtime 8u74)。如果您遇到错误或奇怪的行为,您应该在问题中包含编译错误或运行时堆栈跟踪以及意外行为的描述和环境的详细描述(使用的 JDK 版本等)。
-
最好编辑问题并将格式化的堆栈跟踪放置在其中而不是在 cmets 中。
标签: java javafx duration timeline keyframe