猿问

网格窗格中的 JavaFX 1 第一列比其他列间隔得更远

在我的程序中,我尝试将一个填充了 CheckBox 的 HBox 输出到屏幕上。但是,当我运行该程序时,与其他复选框相比,CheckBox "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;

    }

编辑:这是我在说什么的截图

BIG阳
浏览 144回答 2
2回答

拉莫斯之舞

private Scene assets(Stage primaryStage){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scene assets;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GridPane gp = new GridPane();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.setVgap(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //gp.setPadding(new Insets(25, 0, 25, 25));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text title = new Text("Assets");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title.setFont(Font.font("Arial", FontWeight.BOLD, 14));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.add(title, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text description = new Text("Please select all assets you would like to include in your budget");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.add(description, 0, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String [] optionsString = new String []{"A", "B", "C", "D", "E", "F"};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HBox checkboxContainer = new HBox();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxContainer.setPadding(new Insets(5, 5, 5, 5));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxContainer.setSpacing(20);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < optionsString.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int column = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int row = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String option = optionsString[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBox checkBox = new CheckBox(option);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChoiceBox<Integer> choice = new ChoiceBox<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label label = new Label("How many " + optionsString[i] + " options do you have?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; choice.getItems().addAll(1, 2, 3, 4, 5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HBox choiceContainer = new HBox(label, choice);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (newValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.add(choiceContainer, 0, row + 4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.getChildren().remove(choiceContainer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkboxContainer.getChildren().add(checkBox);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gp.add(checkboxContainer, 0, 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assets = new Scene (gp, 1280, 720);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return assets;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }CheckboxContainer 必须在 for 循环之外。

GCT1015

您无意中将第 0 列(第一列)的宽度设置为文本的宽度,“请全选……等等”。这是因为 GridPane 使用列的最大元素的首选宽度作为该列的宽度。您可能希望从 GridPane 中删除文本并将其作为 HBox 或 VBox 的一部分,而 GridPane 也是其中的一个元素。感觉这是最自然的解决方案。要么是那样,要么你将不得不在文本元素的跨度上胡闹,所以 GridPane 认为它应该跨多个列。GridPanes 最适合自然地可以被认为是网格的数据,其中每列的数据宽度相似。那不是你所拥有的。然而,您可以强制 GridPane 通过一点点反复试验来做几乎任何事情。
随时随地看视频慕课网APP

相关分类

Java
我要回答