![Mastering JavaFX 10](https://wfqqreader-1252317822.image.myqcloud.com/cover/811/36699811/b_36699811.jpg)
上QQ阅读APP看书,第一时间看更新
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=1738865295-I58ah6bKAYRw3GpqS12nya2nixY4gxLE-0-54f3cafefd0419d698631e97c6ba9208)
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=1738865295-m7gQW7zpzSKMqoy3FoKXIpkHjc3RyYe9-0-a9ca91a310c061d504ad2a62655868f5)