JavaFX 在 BorderPane 中添加和删除节点

JavaFX adding and removing Node from BorderPane

我想在 BorderPane 中以编程方式添加和删除侧边菜单。 问题是当我添加 Node 时,它不可见。 BorderPaneStackPane 在 FXML 文件中定义。

我想做这样的事情:

StackPane temp = (StackPane) borderPane.getChildren().get(1);
borderPane.getChildren().remove(1);
borderPane.getChildren().add(0, temp);

我已经尝试了 borderPane.requestLayout() 但它不起作用。

您可以使用 setRight or setLeft, setTop, setBottom, setCenter methods to add Nodes to different parts and also getRightgetLeftgetTopgetBottomgetCenter 来检索当前分配的 Node。 set 方法也可用于通过传递 null 值来删除当前设置的 Node

示例:

假设您有一个 BorderPane,其中有一个 StackPane 放在右侧,您想要将它移到左侧。

StackPane temp = (StackPane) borderPane.getRight(); // Casting is unnecessary
borderPane.setRight(null);
borderPane.setLeft(temp);

从边框窗格中删除节点非常简单。声明边框窗格 ID 并使用该 ID select 子节点并通过以下代码删除指定的边(左、右、上、下)。

    @FXML
    private BorderPane borderPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        borderPane.getChildren().remove(borderPane.getLeft());
        borderPane.getChildren().remove(borderPane.getRight());
    }