【问题标题】:Adding a GridPane to a GridPane将 GridPane 添加到 GridPane
【发布时间】:2015-11-23 20:29:32
【问题描述】:

我是使用 JavaFX 的初学者,必须为最终项目创建交互式数独板。我的计划是将九个 3 x 3 GridPane 添加到一个 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 中的某些列和行之间添加边框?

【问题讨论】:

    标签: user-interface javafx border sudoku gridpane


    【解决方案1】:

    看起来您正在创建9x9=81“内部”网格窗格,而您应该创建其中的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);
    
        }
    }
    

    样式设置只是适当地调整文本字段的大小,并在每个“块”(即“内部网格窗格”)周围放置一个黑色边框。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2015-12-29
      相关资源
      最近更新 更多