2016-11-17 289 views
1

我想以编程方式在BorderPane中添加和删除侧面菜单。 问题是当我添加一个Node时,它不可见。在FXML文件中定义了 BorderPaneStackPaneJavaFX从BorderPane添加和删除节点

我想要做这样的事情:

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

我试图borderPane.requestLayout(),但它无法正常工作。

回答

0

您可以使用setRightsetLeftsetTopsetBottomsetCenter方法来添加Node s到不同的部分,也getRightgetLeftgetTopgetBottomgetCenter检索当前分配Node。设置的方法也可以用于通过传递null值来删除当前设置的Node

例子:

假设你有一个BorderPane具有StackPane放置在右侧,并且希望将其移动到左侧。

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

谢谢,那就是我一直在寻找的东西。 –

+0

当我使用'borderPane.setAligment()'时,为什么我建议使用static?我宁愿这样定位我的节点 – Lealo