设置某些特定跨度配置时,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 中的空白区域)