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

标签 java javafx

每个堆栈 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/

相关文章:

java - OpenGL:使用顶点数组渲染数千个立方体,效果不太好

java - 是否可以在javafx中的对话框上使用路径转换?

java - 用于隐藏 Java Web 服务的身份验证详细信息的外观

java - Spark : How to obtain the location of configurations spark is using?

Java:计算内存使用量时结果不准确

java - org.apache.jena.rdf.model 不存在

java - 打破听众的递归

controller - 具有多个不同 Controller 的 JavaFX 1 FXML 文件?

java - 尝试在我的 Controller 中使用原始版本和全屏版本的 Pane 也会更改窗口版本中的 Pane

JavaFX 坐标困惑