JavaFX: Set Scene min size excluding decorations

前端 未结 2 1522
小鲜肉
小鲜肉 2021-01-19 12:47

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

相关标签:
2条回答
  • 2021-01-19 13:16

    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:

    0 讨论(0)
  • 2021-01-19 13:24

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

    primaryStage.initStyle(StageStyle.UNDECORATED);
    
    0 讨论(0)
提交回复
热议问题