gpt4 book ai didi

java - 堆栈 Pane 中的底部节点消失

转载 作者:行者123 更新时间:2023-11-30 01:47:38 26 4
gpt4 key购买 nike

每个堆栈 Pane 应该有两个节点。但是,底部节点在堆栈 Pane 数组中前 2 个索引的 for 循环之外消失。

public class Main extends Application {
GridPane images;
StackPane[] stackPane;
ImageView cardBack;
ImageView[] cardImages;

@Override
public void start(Stage stage) throws FileNotFoundException {
images = new GridPane(); images.setAlignment(Pos.CENTER);
images.setVgap(5); images.setHgap(5);

cardBack = new ImageView(new Image(new FileInputStream("images/b1fv.gif")));
cardImages = new ImageView[]{
new ImageView(new Image(new FileInputStream("images/c1.gif"))),
new ImageView(new Image(new FileInputStream("images/c2.gif"))),
new ImageView(new Image(new FileInputStream("images/c3.gif")))
};

final Button[] flip = new Button[cardImages.length];
stackPane = new StackPane[cardImages.length];

for (int i = 0; i < cardImages.length; i++) {
stackPane[i] = new StackPane();
stackPane[i].getChildren().addAll(cardBack, cardImages[i]);

images.add(stackPane[i], i, 0, 1, 1);
flip[i] = new Button("Flip");
GridPane.setHalignment(flip[i], HPos.CENTER);
images.add(flip[i], i, 1, 1, 1);

// Debug
System.out.println(stackPane[i].getChildren().toString());

final int j = i;
flip[j].setOnAction(event -> doFlip(j));
}

// Debug
System.out.println("");
for (StackPane pane : stackPane)
System.out.println(pane.getChildren().toString());

stage.setTitle("Assignment 11");
stage.setScene(new Scene(images, 500,200));
stage.show();
}

void doFlip(int loc) {
// Debug
System.out.println("");
for (StackPane pane : stackPane)
System.out.println(pane.getChildren().toString());

ObservableList<Node> children = stackPane[loc].getChildren();
Node topNode = children.get(children.size()-1);
topNode.toBack();
}

public static void main(String[] args) {
launch(args);
}
}
[ImageView@5e1bc54a[styleClass=image-view], ImageView@25389181[styleClass=image-view]]
[ImageView@5e1bc54a[styleClass=image-view], ImageView@ff7cf97[styleClass=image-view]]
[ImageView@5e1bc54a[styleClass=image-view], ImageView@18b8669d[styleClass=image-view]]

[ImageView@25389181[styleClass=image-view]]
[ImageView@ff7cf97[styleClass=image-view]]
[ImageView@5e1bc54a[styleClass=image-view], ImageView@18b8669d[styleClass=image-view]]

[ImageView@25389181[styleClass=image-view]]
[ImageView@ff7cf97[styleClass=image-view]]
[ImageView@5e1bc54a[styleClass=image-view], ImageView@18b8669d[styleClass=image-view]]

在 for 循环内它显示所有对象。但是,循环之外的 in 不显示除数组最后一个元素之外的底部节点。

最佳答案

您不能将同一个 Node 实例添加到多个父节点!

在循环的顶部有以下行:

stackPane[i].getChildren().addAll(cardBack, cardImages[i]);

cardBack 节点将添加到所有 StackPanes 中,但由于它只能有一个父节点,因此将从前一个节点中删除。 Javafx 提供了一种重用图像资源的方法(我想这就是您想要实现的目标)。您可以缓存 ImageView 构造函数中传递的 Image 实例,而不是创建一个 ImageView。例如:

cardBack = new Image(new FileInputStream("images/b1fv.gif"));
// some stuff
for (int i = 0; i < cardImages.length; i++) {
stackPane[i] = new StackPane();
stackPane[i].getChildren().addAll(new ImageView(cardBack) , cardImages[i]);
// rest of the loop
}

如果您如上所述使用 ImageImageView,一切都会正常工作。

关于java - 堆栈 Pane 中的底部节点消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com