JavaFX 8 - GridPane自动调整问题

时间:2015-02-05 16:08:53

标签: javafx-8 gridpane

在设置某些特定范围配置时,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大小调整策略?

案例1:好的

enter image description here

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

改变这个:

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

对此:

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

你会看到

Image without extra columns

理想情况下,如果您计划只有一列,则应删除该colspan和rowspan,然后将其保留:

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

如果保持列平衡,GridPane的宽度将是可预测的,因为它将被计算为:insets + spacing +每列宽度

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

希望这有帮助!