JavaFX: Set Scene min size excluding decorations

这一生的挚爱 提交于 2019-12-31 02:26:08

问题


I know that with JavaFX you can set the Stage min size using stage.setMinWidth() and stage.setMinHeight(), however, this will include the window border (with the minimize, maximize, and close buttons).

How can I exclude this when setting the minimum size?


回答1:


You can show the Stage undecorated, which will remove all the borders:

primaryStage.initStyle(StageStyle.UNDECORATED);

If you want to have it decorated

If you want to have it decorated you can set the size of the Scene to the preferred minimum size, then show the window (the Stage will have the size to be able to display the Scene with that size which one actually is your needed minimum size), then set the minimal size to the current size.

Example:

public class Main extends Application {

    private final int PREF_MIN_WIDTH = 500;
    private final int PREF_MIN_HEIGHT = 500;

    @Override
    public void start(Stage primaryStage) {
        try {
            Scene scene = new Scene(new HBox(), PREF_MIN_WIDTH, PREF_MIN_HEIGHT);

            primaryStage.setScene(scene);
            primaryStage.showingProperty().addListener((observable, oldValue, showing) -> {
                if(showing) {
                    primaryStage.setMinHeight(primaryStage.getHeight());
                    primaryStage.setMinWidth(primaryStage.getWidth());
                    primaryStage.setTitle("My mininal size is: W"+ primaryStage.getMinWidth()+" H"+primaryStage.getMinHeight());
                }
            });

            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

This will produce a window like this:




回答2:


Inside void start(Stage primaryStage), put the following:

primaryStage.initStyle(StageStyle.UNDECORATED);


来源:https://stackoverflow.com/questions/37603939/javafx-set-scene-min-size-excluding-decorations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!