如果舞台设置了 minWidth/minHeight,JavaFX 场景不会调整大小以填充舞台

问题描述 投票:0回答:1

我需要在给定窗口上设置最小宽度和最小高度。

我发现,如果我这样做,那么当窗口首次显示时,场景不会填充父级。如果您手动拉伸窗口,它会正确重新绘制。

仅当 minWidth = width 且 minHeight = height 时才会出现这种情况。在所有其他情况下,它都会按预期调整大小(例如 minWidth = width -1)。

此外,这种情况似乎仅发生在 Linux(特别是 Red Hat 6)上,并且仅发生在 Java 8 上。 我找不到此行为的任何现有错误报告。

我正在寻找解决此问题的方法。这是一个遗留项目,不幸的是无法更新 Java。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class SceneDoesntFillStage extends Application {

    public static void main(String\[\] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setMinWidth(300);
        primaryStage.setWidth(300);

        primaryStage.setMinHeight(200);
        primaryStage.setHeight(200);

        HBox root = new HBox() {
            protected void layoutChildren() {
                super.layoutChildren();
                System.out.println("Layout");
            }
        };
        root.setStyle("-fx-background-color:red");
        root.setPrefWidth(250);
        root.setPrefHeight(150);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();

        System.out.println(primaryStage.getScene().getWidth() + " x " + primaryStage.getScene().getHeight());
    }

}

1

javafx javafx-8
1个回答
0
投票

这个问题很老了,但无论如何......你可以尝试这个:

root.prefHeightProperty().bind(scene.heightProperty());
root.prefWidthProperty().bind(scene.widthProperty());

还要删除这个:

root.setPrefWidth(250);
root.setPrefHeight(150);
© www.soinside.com 2019 - 2024. All rights reserved.