如何在 javaFX 中复制窗格?

问题描述 投票:0回答:0

我需要在 javaFX 中复制一个窗格并给它一个新的 id 以供独立使用。 这个项目有一个 App.java 和 Interfaz.java,“Interfaz.java”是 FXML 控制器。 这是 .FXML 文件中的窗格 我使用 Scenebuilder 创建 FXML 文件。

<Pane fx:id="B1" layoutX="100.0" layoutY="100.0" prefHeight="45.0" prefWidth="45.0">
         <children>
            <Rectangle arcHeight="5.0" fill="#333333" height="45.0" stroke="BLACK" strokeType="INSIDE" width="45.0" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="39.0" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="38.0" layoutX="37.5" layoutY="7.0" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="45.0" layoutX="7.5" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="45.0" layoutX="15.0" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="45.0" layoutX="30.0" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="45.0" layoutX="22.5" stroke="BLACK" strokeType="INSIDE" width="7.5" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="7.5" layoutY="37.5" stroke="BLACK" strokeType="INSIDE" width="38.0" />
            <Rectangle arcHeight="5.0" fill="#c98955" height="7.5" layoutX="7.5" stroke="BLACK" strokeType="INSIDE" width="37.5" />
            <Circle fill="#333333" layoutX="41.5" layoutY="3.5" radius="1.0" stroke="TRANSPARENT" strokeType="INSIDE" />
            <Circle fill="#333333" layoutX="3.5" layoutY="3.5" radius="1.0" stroke="TRANSPARENT" strokeType="INSIDE" />
            <Circle fill="#333333" layoutX="41.5" layoutY="41.5" radius="1.0" stroke="TRANSPARENT" strokeType="INSIDE" />
            <Circle fill="#333333" layoutX="3.5" layoutY="41.5" radius="1.0" stroke="TRANSPARENT" strokeType="INSIDE" />
         </children>
      </Pane>

并且此 Pane 是具有 fixId“padre”的 AnchorPane 的子级。 我使用此代码在 App.java 中显示场景

private static Scene scene;

@Override
public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("Interfaz"), 800, 600);
        stage.setScene(scene);
        stage.show();
    }

this is what i get runing the proyect at this point

我试过用这个

AnchorPane padre = (AnchorPane) scene.lookup("#padre");
Pane original = (Pane) padre.lookup("#B1");
Pane copia = new Pane();
copia.setLayoutX(original.getLayoutX());
copia.setLayoutY(original.getLayoutY());
copia.setPrefWidth(original.getPrefWidth());
copia.setPrefHeight(original.getPrefHeight());
ObservableList<Node> children = original.getChildren();
copia.getChildren().addAll(children);
copia.setId("copiaB1");
padre.getChildren().add(copia);

此代码与之前所示的函数相同。 this is what i get using the previus code 然后我添加

copia.setTranslateX(400);
移动新窗格但原始窗格未显示,或者我移动的窗格与原始窗格相同或我不明白的东西。 this is what i get moving the copy

我还尝试在原始窗格和复制窗格中使用 .toFront,但似乎没有任何改变。 我尝试使用 .clone 但访问受保护,我不知道如何使用它。

java maven javafx scenebuilder
© www.soinside.com 2019 - 2024. All rights reserved.