![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=1739382878-66errm08oyGqtX9pqTQ4nfy8Wy1jZxHp-0-42595f750f1596de8b4bca5d5257c165)
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=1739382878-kuZfAd3ykPhmS0LRiUUI58AsqpA3HQRn-0-9033f73538b867d0cae15d0b6c9344d0)