3

设置某些特定跨度配置时,GridPane 自动调整大小存在问题。

以下配置按我的预期工作:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class GridPaneWidthIssue extends Application {

    public static void main(String... arguments) {
        launch(arguments);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();

        BorderStroke borderStroke = new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, CornerRadii.EMPTY,
                BorderWidths.DEFAULT);

        Border border = new Border(borderStroke);
        gridPane.setBorder(border);
        gridPane.setGridLinesVisible(true);
        gridPane.setHgap(2);
        gridPane.setVgap(2);
        gridPane.setPadding(new Insets(10));

        Label column0 = new Label("Column 0");
        Label column1 = new Label("Column 1");
        Label column2 = new Label("Column 2");
        Label column01 = new Label("Span column 0 and 1");
        Label column012 = new Label("Span column 0, 1 and 2");

        gridPane.add(column0, 0, 0);
        gridPane.add(column1, 1, 0);
        gridPane.add(column2, 2, 0);
        gridPane.add(column01, 0, 1, 2, 1);
        gridPane.add(column012, 0, 2, 3, 1);

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我还不能发布图片来证明这一点,但我可以验证以下断言:

**gridpane.width = column0.width + column1.width + column2.width

当我删除第 0 行中的标签时,我得到以下代码:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class GridPaneWidthIssue extends Application {

    public static void main(String... arguments) {
        launch(arguments);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();

        BorderStroke borderStroke = new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, CornerRadii.EMPTY,
                BorderWidths.DEFAULT);

        Border border = new Border(borderStroke);
        gridPane.setBorder(border);
        gridPane.setGridLinesVisible(true);
        gridPane.setHgap(2);
        gridPane.setVgap(2);
        gridPane.setPadding(new Insets(10));

        Label column0 = new Label("Column 0");
        Label column1 = new Label("Column 1");
        Label column2 = new Label("Column 2");
        Label column01 = new Label("Span column 0 and 1");
        Label column012 = new Label("Span column 0, 1 and 2");

        // gridPane.add(column0, 0, 0);
        // gridPane.add(column1, 1, 0);
        // gridPane.add(column2, 2, 0);
        gridPane.add(column01, 0, 1, 2, 1);
        gridPane.add(column012, 0, 2, 3, 1);

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

结果不正确:

gridpane.width > column0.width + column1.width + column2.width

我没有验证它,但我猜它是:

gridpane.width = column0.width + column1.width + column0.width + column1.width + column2.width

JavaFX 问题还是我误解了 Gridpane 大小调整策略?

案例一:好的

在此处输入图像描述

案例 2:KO(GridPane 中的空白区域)

在此处输入图像描述

4

1 回答 1

0

您会看到 GridPane 右侧的额外空间,因为添加标签时的 colspan 为 3,添加column012标签时的 colspan 为 2 column01(您不再有三列,而只有一列)。请记住 GridPane 是一个表,您需要保持它的列平衡并跨越它的列/行限制。在您的情况下,空白空间只是 GridPane 内任何标签无法触及的第三列。

改变这个:

gridPane.add(column012, 0, 2, 3, 1);

对此:

gridPane.add(column012, 0, 2, 2, 1);

你会看到

没有额外列的图像

理想情况下,如果您计划只有一列,则应删除该 colspan 和 rowspan 并保留以下内容:

gridPane.add(column01, 0, 1);
gridPane.add(column012, 0, 2);

如果您保持列平衡,则 GridPane 的宽度将是可预测的,因为它将被计算为:插入 + 间隙 + 每列宽度

您可能还想查看ColumnConstraints类以根据自己的喜好自定义每个列的行为。

希望这可以帮助!

于 2015-08-19T20:53:37.557 回答