【发布时间】:2019-08-15 23:22:52
【问题描述】:
我想一个接一个地显示不同的 gif。
我的想法是使用时间轴动态更改包装到图像中的显示 gif。
但我得到一个错误:
从 lambda 表达式引用的局部变量必须是 final 或 有效地最终
有什么解决办法吗?
我的代码:
public class Task10 extends Application {
@Override
public void start(Stage primaryStage) {
HBox hbox5 = new HBox();
VBox VBoxAll = new VBox();
Image gifs[] = new Image[3];
gifs[0] = new Image(this.getClass().getResource("/img/L1.gif").toExternalForm());
gifs[1] = new Image(this.getClass().getResource("/img/L2.gif").toExternalForm());
gifs[2] = new Image(this.getClass().getResource("/img/L1.gif").toExternalForm());
ImageView currentGif = new ImageView();
Button localLoadButton = new Button("Start!");
localLoadButton.setOnAction(e -> {
show(currentGif, gifs);
});
hbox5.getChildren().addAll(currentGif, localLoadButton);
VBoxAll.getChildren().addAll(hbox5);
VBoxAll.setSpacing(15);
Pane pane = new Pane();
pane.getChildren().add(VBoxAll);
Scene scene = new Scene(pane, 500, 350);
primaryStage.setScene(scene);
primaryStage.show();
}
public void show(ImageView currentGif, Image[] gifs) {
for (int i = 0; i<gifs.length; i++) {
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, e -> { currentGif.setImage(gifs[i]); }),
new KeyFrame(Duration.seconds(2), e -> { currentGif.setImage(null); })
);
timeline.play();
}
}
}
【问题讨论】:
-
请粘贴整个类代码(带导入)。我在编译时遇到了问题。
标签: java javafx javafx-8 gif animated-gif