The
的
GridPane
class creates a
JavaFX layout pane which places controls based on a column and row position. The grid contained in this layout is not predefined. It creates columns and rows as each control is added. This allows the grid to be completely flexible in its des
JavaFX布局窗格,该窗格根据列和行的位置放置控件。 此布局中包含的网格未预定义。 在添加每个控件时,它会创建列和行。 这使得网格在其设计中完全灵活
Nodes can be placed in each cell of the grid and can span multiple cells either vertically or horizontally. By default the rows and columns will be sized to fit their content - that is the widest child node defines the column width and the tallest child node the row height.
节点可以放置在网格的每个单元中,并且可以垂直或水平地跨越多个单元。 默认情况下,行和列的大小将适合其内容-即最宽的子节点定义列的宽度,而最高的子节点定义行的高度。
进口声明 ( Import Statement )
import javafx.scene.layout.GridPane;
建设者 ( Constructors )
The
的
GridPane
class has one constructor which does not accept any argume
GridPane playerGrid = new GridPane();
有用的方法 ( Useful Methods )
Child nodes are added to the
子节点被添加到
GridPane
using the add method specifying the node to be added with the column and row in
//Place the Text control in column 1, row 8
Text rank4 = new Text("4");
playerGrid.add(rank4, 0,7);
Note: The column and row index starts at 0. So the first cell positioned at column 1, row 1 has an index of 0, 0.
注意:列和行的索引从0开始。因此,位于第1列,第1行的第一个单元格的索引为0、0。
Child nodes can also span multiple columns or rows. This can be specified in the
子节点也可以跨越多个列或行。 可以在
add
method by adding the number of columns and rows to span to the end of the arguments pas
//Here the Text control is spanning 4 columns and 1 row
Text title = new Text("Top Scorers in English Premier League");
playerGrid.add(title, 0,0,4,1);
Child nodes contained within the
包含在
GridPane
can have their alignment along the horizontal or vertical axis by using the
setHalignment
and
setValignment
meth
GridPane.setHalignment(goals4, HPos.CENTER);
Note: The
注意:
VPos
enum contains four constant values to define the vertical position:
BASELINE
,
BOTTOM
,
CENTER
and
TOP
. The
HPos
enum only contains three values for the horizontal position:
CENTER
,
LEFT
and
RIGHT
The padding of child nodes can also be set by using the
Insets
object defining the padd
The spacing between the columns and rows can be defined by using the
setHgap
and playerGrid.setHgap(10);
playerGrid.setVgap(10);
The
playerGrid.setGridLinesVisible(true);
Usage Tips
If two nodes are set to be displayed in the same cell then they will overlap in the JavaFX scene.
Columns and rows can be set to a preferred width and height through the use of
ColumnConstraints
. These are separate classes that can be used to control the size. Once defined they are added to the getRowConstraints().addAll
and GridPane
objects can be styled using JavaFX CSS. All the CSS properties defined under
To see the
GridPane
layout in action have a look at the GridPane Example Program. It shows how to place
RIGHT
The padding of child nodes can also be set by using the
Insets
object defining the padd
The spacing between the columns and rows can be defined by using the
setHgap
and playerGrid.setHgap(10);
playerGrid.setVgap(10);
The
playerGrid.setGridLinesVisible(true);
Usage Tips
If two nodes are set to be displayed in the same cell then they will overlap in the JavaFX scene.
Columns and rows can be set to a preferred width and height through the use of
ColumnConstraints
. These are separate classes that can be used to control the size. Once defined they are added to the getRowConstraints().addAll
and GridPane
objects can be styled using JavaFX CSS. All the CSS properties defined under
To see the
GridPane
layout in action have a look at the GridPane Example Program . It shows how to place