上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));
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: