Mastering JavaFX 10
上QQ阅读APP看书,第一时间看更新

GridPane layout manager

GridPane is most complex layout manager; it allows users to sets rows and columns where child Nodes can be placed. 

You can control a lot of constraints through the API: grow strategy, the relative and absolute sizes of columns and rows, resize policy, and so on. I won't go through all of them to avoid repeating JavaDoc, but will show only a short sample—let's make a small chessboard pattern using GridPane:

GridPane root = new GridPane();
for (int i = 0; i < 5; i++) {
root.getColumnConstraints().add(new ColumnConstraints(50));
root.getRowConstraints().add(new RowConstraints(50));
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((i+j)%2 == 0)
root.add(new Rectangle(30, 30, Color.BLUE), i, j);
}
}

We get the following output: