JavaFX从BorderPane添加和删除节点
问题描述:
我想以编程方式在BorderPane
中添加和删除侧面菜单。 问题是当我添加一个Node
时,它不可见。在FXML文件中定义了 BorderPane
和StackPane
。JavaFX从BorderPane添加和删除节点
我想要做这样的事情:
StackPane temp = (StackPane) borderPane.getChildren().get(1);
borderPane.getChildren().remove(1);
borderPane.getChildren().add(0, temp);
我试图borderPane.requestLayout()
,但它无法正常工作。
答
您可以使用setRight
或setLeft
,setTop
,setBottom
,setCenter
方法来添加Node
s到不同的部分,也getRight
,getLeft
,getTop
,getBottom
,getCenter
检索当前分配Node
。设置的方法也可以用于通过传递null
值来删除当前设置的Node
。
例子:
假设你有一个BorderPane
具有StackPane
放置在右侧,并且希望将其移动到左侧。
StackPane temp = (StackPane) borderPane.getRight(); // Casting is unnecessary
borderPane.setRight(null);
borderPane.setLeft(temp);
谢谢,那就是我一直在寻找的东西。 –
当我使用'borderPane.setAligment()'时,为什么我建议使用static?我宁愿这样定位我的节点 – Lealo