将 GridPane 添加到 GridPane
Adding a GridPane to a GridPane
我是使用 JavaFX 的初学者,必须为最终项目创建交互式数独板。我的计划是将九个 3 x 3 GridPanes 添加到一个 3 x 3 GridPane(以制作带有轮廓框的漂亮板)但似乎无法像我那样做。
这是我的 blankBoard() 创建方法的摘录。 board 和 box 预定义为全局 GridPane 变量:
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
for (int row1 = 0; row1 < 3; row1++) {
for (int col1 = 0; col1 < 3; col1++) {
text = new TextField("0");
box = new GridPane();
GridPane.setConstraints(text, col1, row1);
box.getChildren().addAll(text);
}
}
GridPane.setConstraints(box, col, row);
board.getChildren().addAll(box);
}
}
所有这些给我的是一个 3 x 3 的 GridPane:
GridPane
有没有其他方法可以做到这一点,或者只是在 9 x 9 的 GridPane 中的某些列和行之间添加边框?
看起来您正在创建 9x9=81
"inner" 个网格窗格,而您应该创建 3x3=9
个网格窗格。然后你只将每组 9 个中的最后一个添加到板上。你需要像
这样的东西
GridPane board = new GridPane();
for (int blockColumn = 0; blockColumn < 3 ; blockColumn++) {
for (int blockRow = 0; blockRow < 3; blockRow++) {
GridPane box = new GridPane();
box.setStyle("-fx-background-color: black, -fx-control-inner-background; -fx-background-insets: 0, 2; -fx-padding: 2;");
for (int column = 0; column < 3; column++) {
for (int row = 0 ; row < 3; row++) {
TextField textField = new TextField("0");
textField.setStyle("-fx-pref-width: 2em;");
GridPane.setConstraints(textField, column, row);
box.getChildren().add(textField);
}
}
GridPane.setConstraints(box, blockColumn, blockRow);
board.getChildren().add(box);
}
}
样式设置只是适当调整文本字段的大小,并在每个 "blocks"(即 "inner grid panes")周围放置一个黑色边框。
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class SudokuBoard extends Application {
@Override
public void start(Stage primaryStage) {
GridPane board = new GridPane();
for (int blockColumn = 0; blockColumn < 3 ; blockColumn++) {
for (int blockRow = 0; blockRow < 3; blockRow++) {
GridPane box = new GridPane();
box.setStyle("-fx-background-color: black, -fx-control-inner-background; -fx-background-insets: 0, 2; -fx-padding: 2;");
for (int column = 0; column < 3; column++) {
for (int row = 0 ; row < 3; row++) {
TextField textField = new TextField("0");
textField.setStyle("-fx-pref-width: 2em;");
GridPane.setConstraints(textField, column, row);
box.getChildren().add(textField);
}
}
GridPane.setConstraints(box, blockColumn, blockRow);
board.getChildren().add(box);
}
}
primaryStage.setScene(new Scene(board));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我是使用 JavaFX 的初学者,必须为最终项目创建交互式数独板。我的计划是将九个 3 x 3 GridPanes 添加到一个 3 x 3 GridPane(以制作带有轮廓框的漂亮板)但似乎无法像我那样做。
这是我的 blankBoard() 创建方法的摘录。 board 和 box 预定义为全局 GridPane 变量:
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
for (int row1 = 0; row1 < 3; row1++) {
for (int col1 = 0; col1 < 3; col1++) {
text = new TextField("0");
box = new GridPane();
GridPane.setConstraints(text, col1, row1);
box.getChildren().addAll(text);
}
}
GridPane.setConstraints(box, col, row);
board.getChildren().addAll(box);
}
}
所有这些给我的是一个 3 x 3 的 GridPane: GridPane
有没有其他方法可以做到这一点,或者只是在 9 x 9 的 GridPane 中的某些列和行之间添加边框?
看起来您正在创建 9x9=81
"inner" 个网格窗格,而您应该创建 3x3=9
个网格窗格。然后你只将每组 9 个中的最后一个添加到板上。你需要像
GridPane board = new GridPane();
for (int blockColumn = 0; blockColumn < 3 ; blockColumn++) {
for (int blockRow = 0; blockRow < 3; blockRow++) {
GridPane box = new GridPane();
box.setStyle("-fx-background-color: black, -fx-control-inner-background; -fx-background-insets: 0, 2; -fx-padding: 2;");
for (int column = 0; column < 3; column++) {
for (int row = 0 ; row < 3; row++) {
TextField textField = new TextField("0");
textField.setStyle("-fx-pref-width: 2em;");
GridPane.setConstraints(textField, column, row);
box.getChildren().add(textField);
}
}
GridPane.setConstraints(box, blockColumn, blockRow);
board.getChildren().add(box);
}
}
样式设置只是适当调整文本字段的大小,并在每个 "blocks"(即 "inner grid panes")周围放置一个黑色边框。
SSCCE:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class SudokuBoard extends Application {
@Override
public void start(Stage primaryStage) {
GridPane board = new GridPane();
for (int blockColumn = 0; blockColumn < 3 ; blockColumn++) {
for (int blockRow = 0; blockRow < 3; blockRow++) {
GridPane box = new GridPane();
box.setStyle("-fx-background-color: black, -fx-control-inner-background; -fx-background-insets: 0, 2; -fx-padding: 2;");
for (int column = 0; column < 3; column++) {
for (int row = 0 ; row < 3; row++) {
TextField textField = new TextField("0");
textField.setStyle("-fx-pref-width: 2em;");
GridPane.setConstraints(textField, column, row);
box.getChildren().add(textField);
}
}
GridPane.setConstraints(box, blockColumn, blockRow);
board.getChildren().add(box);
}
}
primaryStage.setScene(new Scene(board));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}