【问题标题】:JavaFX 1 first column in gridpane is spaced out much farther than the rest网格窗格中的 JavaFX 1 第一列的间距比其他列远得多
【发布时间】:2019-02-12 01:30:41
【问题描述】:

在我的程序中,我尝试将一个填充有 CheckBox 的 HBox 输出到屏幕上。但是,当我运行程序时,与其他复选框相比,复选框“A”的间距要远得多。

这是我的代码:

private Scene assets (Stage primaryStage){

        GridPane gp = new GridPane();
        gp.setVgap(5);
        gp.setPadding(new Insets(25, 25, 25, 25));

        Text title = new Text("Assets");
        title.setFont(Font.font("Arial", FontWeight.BOLD, 14));
        gp.add(title, 0, 0);

        Text description = new Text("Please select all assets you would like to include in your budget");
        gp.add(description, 0, 1);

        String [] optionsString = new String []{"A", "B", "C", "D", "E", "F"};

        for (int i = 0; i < optionsString.length; i++) {
            final int column = i;
            final int row = i;
            String option = optionsString[i];
            CheckBox checkBox = new CheckBox(option);

            HBox checkboxContainer = new HBox(checkBox);
            checkboxContainer.setSpacing(20);

            ChoiceBox<Integer> choice = new ChoiceBox<>();
            Label label = new Label("How many " + optionsString[i] + " options do you have?");
            choice.getItems().addAll(1, 2, 3, 4, 5);

            HBox choiceContainer = new HBox(label, choice);

            checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
                if (newValue) {
                    gp.add(choiceContainer, 0, row + 4);
                } else {
                    gp.getChildren().remove(choiceContainer);
                }
            });
            gp.add(checkboxContainer, column, 3);
        }

        assets = new Scene (gp, 1280, 720);

        return assets;
    }

编辑:这是我所说的截图

【问题讨论】:

  • 不,我添加了一个屏幕截图来帮助可视化我的问题。
  • 文本不可调整大小且不跨越多列。由于列大小由该列中节点的最大大小确定,因此第一列具有Text 节点的...

标签: java javafx


【解决方案1】:
private Scene assets(Stage primaryStage){
                Scene assets;
                GridPane gp = new GridPane();
                gp.setVgap(0);
                //gp.setPadding(new Insets(25, 0, 25, 25));

                Text title = new Text("Assets");
                title.setFont(Font.font("Arial", FontWeight.BOLD, 14));
                gp.add(title, 0, 0);

                Text description = new Text("Please select all assets you would like to include in your budget");
                gp.add(description, 0, 1);

                String [] optionsString = new String []{"A", "B", "C", "D", "E", "F"};
                HBox checkboxContainer = new HBox();
                checkboxContainer.setPadding(new Insets(5, 5, 5, 5));
                checkboxContainer.setSpacing(20);

                for (int i = 0; i < optionsString.length; i++) {
                    final int column = i;
                    final int row = i;
                    String option = optionsString[i];
                    CheckBox checkBox = new CheckBox(option);
                    ChoiceBox<Integer> choice = new ChoiceBox<>();
                    Label label = new Label("How many " + optionsString[i] + " options do you have?");
                    choice.getItems().addAll(1, 2, 3, 4, 5);

                    HBox choiceContainer = new HBox(label, choice);

                    checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
                        if (newValue) {
                            gp.add(choiceContainer, 0, row + 4);
                        } else {
                            gp.getChildren().remove(choiceContainer);
                        }
                    });
                    checkboxContainer.getChildren().add(checkBox);
                }
                gp.add(checkboxContainer, 0, 2);

                assets = new Scene (gp, 1280, 720);

                return assets;
            }

CheckboxContainer 必须在 for 循环之外。

【讨论】:

    【解决方案2】:

    您无意中将第 0 列(第一列)的宽度设置为文本的宽度,“请全选……等等”

    这是因为 GridPane 使用列的最大元素的首选宽度作为该列的宽度。

    您可能希望从 GridPane 中删除文本,并使其成为 GridPane 也是其元素的 HBox 或 VBox 的一部分。这感觉是最自然的解决方案。

    要么这样,要么你将不得不对文本元素的跨度进行欺骗,因此 GridPane 认为它应该跨越多个列。

    GridPanes 最适合自然地被视为网格的数据,其中每一列的数据宽度相似。那不是你所拥有的。

    但是,您可以强制 GridPane 执行几乎任何操作,只需稍加尝试和错误即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 2020-03-14
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多