JavaFX1 gridpane中的第一列的间距比其他列远得多

JavaFX1 gridpane中的第一列的间距比其他列远得多,java,javafx,Java,Javafx,在我的程序中,我尝试将一个充满复选框的HBox输出到屏幕上。但是,当我运行程序时,复选框“A”的间距比其他复选框的间距大得多 这是我的密码: private Scene assets (Stage primaryStage){ GridPane gp = new GridPane(); gp.setVgap(5); gp.setPadding(new Insets(25, 25, 25, 25)); Text title = n

在我的程序中,我尝试将一个充满复选框的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;
    }
专用场景资源(舞台主舞台){
GridPane gp=新的GridPane();
总成设置间隙(5);
总成设置填充(新插图(25,25,25,25));
文本标题=新文本(“资产”);
title.setFont(Font.Font(“Arial”,fontwweight.BOLD,14));
添加总成(标题,0,0);
Text description=新文本(“请选择您希望包含在预算中的所有资产”);
总成添加(说明,0,1);
字符串[]选项字符串=新字符串[]{“A”、“B”、“C”、“D”、“E”、“F”};
对于(int i=0;i{
如果(新值){
gp.add(choiceContainer,0,行+4);
}否则{
gp.getChildren().remove(choiceContainer);
}
});
总成添加(checkboxContainer,第3列);
}
资产=新场景(gp,1280720);
归还资产;
}
编辑:这是我所说内容的截图

专用场景资源(舞台主舞台){
现场资产;
GridPane gp=新的GridPane();
gp.setVgap(0);
//总成设置填充(新插图(25,0,25,25));
文本标题=新文本(“资产”);
title.setFont(Font.Font(“Arial”,fontwweight.BOLD,14));
添加总成(标题,0,0);
Text description=新文本(“请选择您希望包含在预算中的所有资产”);
总成添加(说明,0,1);
字符串[]选项字符串=新字符串[]{“A”、“B”、“C”、“D”、“E”、“F”};
HBox checkboxContainer=新的HBox();
checkboxContainer.setPadding(新插图(5,5,5,5));
checkboxContainer.setSpacing(20);
对于(int i=0;i{
如果(新值){
gp.add(choiceContainer,0,行+4);
}否则{
gp.getChildren().remove(choiceContainer);
}
});
checkboxContainer.getChildren().add(复选框);
}
总成添加(checkboxContainer,0,2);
资产=新场景(gp,1280720);
归还资产;
}

CheckboxContainer必须位于for循环之外

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

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

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

要么这样,要么你就必须摆弄文本元素的跨度,这样GridPane就会认为它应该跨越多个列

网格窗格是最自然的数据,自然可以被认为是一个网格,其中每列的数据宽度相似。那不是你所拥有的


不过,您可以通过一些尝试和错误来强制GridPane执行几乎任何操作

不,我添加了一个屏幕截图来帮助可视化我的问题。文本不可调整大小,也不跨多列。由于列大小由该列中节点的最大大小决定,因此第一列具有
文本
节点的最大值。。。
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;
            }