add()或setConstraints之间是否有区别,是的,Preference还是真正的大区别?
public class example extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Example");
GridPane grid = new GridPane();
Label label = new Label("Example A");
Label label2 = new Label("Example B");
grid.add(label, 0,0);
GridPane.setConstraints(label2, 1, 0);
grid.getChildren().add(label2);
Scene scene = new Scene(grid, 300,200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
GridPane
类中两个方法的实现:
/**
* Adds a child to the gridpane at the specified column,row position.
* This convenience method will set the gridpane column and row constraints
* on the child.
* @param child the node being added to the gridpane
* @param columnIndex the column index position for the child within the gridpane, counting from 0
* @param rowIndex the row index position for the child within the gridpane, counting from 0
*/
public void add(Node child, int columnIndex, int rowIndex) {
setConstraints(child, columnIndex, rowIndex);
getChildren().add(child);
}
/**
* Sets the column,row indeces for the child when contained in a gridpane.
* @param child the child node of a gridpane
* @param columnIndex the column index position for the child
* @param rowIndex the row index position for the child
*/
public static void setConstraints(Node child, int columnIndex, int rowIndex) {
setRowIndex(child, rowIndex);
setColumnIndex(child, columnIndex);
}
add
方法在指定的列,行位置将一个子项添加到网格窗格中。setConstraints
设置包含在网格窗格中的子项的列,行索引。setConstraints
不会添加子项,但指定在GridPane
中如何显示子项。