如何在JavaFX中使用时间线更改图像

如何在JavaFX中使用时间线更改图像,javafx,timeline,Javafx,Timeline,我打算通过在时间轴对象的for循环中添加关键帧来改变鸟类的图像。结果表明,仅显示第一个图像。有人能指出我错在哪里吗。提前谢谢 此外,我注意到在for循环之后必须将计数器“index”重置为0,否则它将生成java.lang.ArrayIndexOutOfBoundsException 包装申请 import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue;

我打算通过在时间轴对象的for循环中添加关键帧来改变鸟类的图像。结果表明,仅显示第一个图像。有人能指出我错在哪里吗。提前谢谢

此外,我注意到在for循环之后必须将计数器“index”重置为0,否则它将生成java.lang.ArrayIndexOutOfBoundsException

包装申请

import javafx.animation.Animation;
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.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;

public class Main extends Application 
{

int索引=0;
@凌驾
公共无效开始(阶段primaryStage){
试一试{
ImageView bgV=新建ImageView();
Image img_BG=新图像(Main.class.getResourceAsStream(“background.png”);
bgV.setImage(img_-BG);
bgV.setEffect(新的BoxBlur());
bgV.setOpacity(0.5);
ImageView t1V=新的ImageView();
Image img_t1=新映像(Main.class.getResourceAsStream(
“t1.png”
));
t1V.setImage(img_t1);
ImageView t2V=新的ImageView();
Image img_t2=新映像(Main.class.getResourceAsStream)(
“t2.png”
));
t2V.setImage(img_t2);
ImageView t3V=新的ImageView();
Image img_t3=新映像(Main.class.getResourceAsStream(
“t3.png”
));
t3V.setImage(img_t3);
组前景=新组(t1V、t2V、t3V);
t1V.setTranslateX(20);
t1V.setTranslateY(200);
t2V.setTranslateX(300);
t2V.setTranslateY(200);
t3V.setTranslateX(550);
t3V.setTranslateY(200);
setEffect(新的DropShadow());
字符串[]
birdfile={“b1.png”、“b2.png”、“b3.png”、“b4.png”、“b5.png”、“b6.png”};
双[]ds={300600900120015001800};
ImageView birdV=newimageview(新图像(Main.class.getResourceAsStream(birdFiles[0]));
群鸟=新群(birdV);
birds.setTranslateX(img_BG.getWidth()-100);
时间线=新时间线();
timeline.setCycleCount(
动画。无限期
); 
KeyFrame[]kframs=新的关键帧[birdFiles.length];

对于(index=0;index您不必在外部声明您的
index
-字段。这也会导致您的问题:无论何时调用
handle
方法,它都会引用您的字段:
index
,您在循环后将其设置为0

因此,您可以将新字段声明为
final
,并将其传递给处理程序:

for (int index = 0; index < birdFiles.length; index++) {
    final int birdIndex = index;
    EventHandler<ActionEvent> onFishined = new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent arg0) {
                    birds.getChildren().setAll(new ImageView(new Image(Main.class.getResourceAsStream(birdFiles[birdIndex]))));
                }

            };
    ...
}
for(int index=0;index
非常感谢,KnusperPudding,它确实解决了问题参见相关:和
for (int index = 0; index < birdFiles.length; index++) {
    final int birdIndex = index;
    EventHandler<ActionEvent> onFishined = new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent arg0) {
                    birds.getChildren().setAll(new ImageView(new Image(Main.class.getResourceAsStream(birdFiles[birdIndex]))));
                }

            };
    ...
}