0
我的目標是創建一個可調整大小,始終爲正方形且包含相同數量的行和列的GridView,使它們的單元格也爲正方形,類似於Reversi或國際象棋棋盤。正方形單元GridPane正方形單元
下面是一個小插圖,網格在內容窗格中水平居中。
我已經嘗試了不同的結合變和佈局的羣衆,但我不能完全得到它的權利。這裏是我的控制器(到目前爲止):
public class Controller {
public HBox contentPane;
public void initialize() {
final int sideLength = 10;
final GridPane gridPane = new GridPane();
gridPane.setStyle("-fx-border-color: red; -fx-border-insets: 2");
HBox.setHgrow(gridPane, Priority.ALWAYS);
for (int i = 0; i < sideLength; i++) {
final ColumnConstraints columnConstraints = new ColumnConstraints(Control.USE_PREF_SIZE, 10, Double.MAX_VALUE);
columnConstraints.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints()
.add(columnConstraints);
final RowConstraints rowConstraints = new RowConstraints(Control.USE_PREF_SIZE, 10, Double.MAX_VALUE);
rowConstraints.setVgrow(Priority.ALWAYS);
gridPane.getRowConstraints()
.add(rowConstraints);
}
contentPane.getChildren().add(gridPane);
for (int i = 0; i < sideLength; i++) {
for (int j = 0; j < sideLength; j++) {
final GameCell child = new GameCell();
GridPane.setRowIndex(child, i);
GridPane.setColumnIndex(child, j);
gridPane.getChildren().add(child);
}
}
}
}
而且細胞,這是應該包含形狀拉泰什,但我沒空,但Circle
S表示現在只是測試它:
這是它的外觀現在: