- Mastering JavaFX 10
- Sergey Grinev
- 172字
- 2021-06-25 21:21:53
Behavioral layout
For these layout managers, you choose the behavior for layouting of your nodes, and they will do the following tasks for you:
- Calculate child nodes' sizes
- Initial positioning of the child nodes
- Reposition nodes if they change their sizes or the layout manager changes its size
The first manager to look at is HBox. It arranges its children in simple rows:
HBox root = new HBox(5);
root.getChildren().addAll(
new Rectangle(50, 50, Color.GREEN),
new Rectangle(75, 75, Color.BLUE),
new Rectangle(90, 90, Color.RED));
![](https://epubservercos.yuewen.com/0A344C/19470392808882006/epubprivate/OEBPS/Images/Chapter_114.jpg?sign=1739363283-20ZXQ6ekLF8EPZRb5nWc3Ci9ABVZ0Cfw-0-e7e50a4332c2ad186e4b25bec3d84a72)
The corresponding VBox does the same for columns.
StackPane positions nodes in its center.
As nodes will overlap here, note that you can control their Z-order using the following APIs:
- Node.toBack() will push it further from the user
- Note.toFront() will bring it to the top position
Take a look at the following example code:
Pane root = new StackPane();
Rectangle red;
root.getChildren().addAll(
new Rectangle(50, 50, Color.GREEN), // stays behind blue and red
new Rectangle(75, 75, Color.BLUE),
red = new Rectangle(90, 90, Color.RED));
red.toBack();
This is the image that it produces:
![](https://epubservercos.yuewen.com/0A344C/19470392808882006/epubprivate/OEBPS/Images/Chapter_63.jpg?sign=1739363283-6T9tafALQPVjP2nmPHsXNx8GIi4dwKvX-0-90479f5649295f23ed2d3a5db6c38fc7)