GridPane
当我遇到以下问题时,我试图使用 a 来布局 JavaFX 阶段。如果我使用适当的约束设置网格并向其添加新实例化StackPane
的 s,则场景、舞台及其内容的默认大小可确保内容可见:
StackPane
但是,如果我在将其添加到之前添加一个 JavaFX CSS 样式,为新实例化的新实例添加一个边框GridPane
,那么默认大小的东西似乎完全崩溃了:
我的代码如下:
public static void main(final String[] args) {
Platform.startup(() -> {});
Platform.runLater(() -> {
final GridPane gridPane = new GridPane();
final Scene scene = new Scene(gridPane);
final Stage stage = new Stage();
stage.setScene(scene);
final List<StackPane> panes = new ArrayList<>();
for (int i = 0; i < 4; i++) {
// Create a new pane with a random background color for
// illustration
final StackPane p = createNewPane();
panes.add(p);
// The addition / removal of the following line affects the
// layout.
p.setStyle("-fx-border-width:2px;-fx-border-color:red");
}
for (int r = 0; r < 2; r++) {
final RowConstraints rc = new RowConstraints();
rc.setPercentHeight(50);
gridPane.getRowConstraints().add(rc);
}
for (int c = 0; c < 2; c++) {
final ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(50);
gridPane.getColumnConstraints().add(cc);
}
for (int r = 0, i = 0; r < 2; r++) {
for (int c = 0; c < 2; c++) {
gridPane.add(panes.get(i++), c, r);
}
}
stage.show();
});
}
奇怪的是,如果我stage.show()
在设置场景后向右移动,那么即使使用 CSS 也一切正常。
谁能帮我理解,一,这是否是预期的行为,二,为什么执行顺序stage.show()
会有所不同?
谢谢!