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

Ada*_*dam 5 javafx javafx-8

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

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

仅当 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());
    }

}
Run Code Online (Sandbox Code Playgroud)

1

小智 0

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

root.prefHeightProperty().bind(scene.heightProperty());
root.prefWidthProperty().bind(scene.widthProperty());
Run Code Online (Sandbox Code Playgroud)

并且还要删除这个:

root.setPrefWidth(250);
root.setPrefHeight(150);
Run Code Online (Sandbox Code Playgroud)