How to make javafx.scene.Scene resize while maintaining an aspect ratio?

后端 未结 2 1098
说谎
说谎 2021-01-06 03:06

I\'m writing an application with JavaFX 2.2.7-b01.

Here is an example of the code I currently have. How can I allow the application window to be resized but maintain

2条回答
  •  长发绾君心
    2021-01-06 03:45

    > How to make javafx.scene.Scene resize while maintaining an aspect ratio?

    By manipulating the stage size instead of scene:

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Play by resizing the window");
        VBox root = new VBox();
        root.getChildren().add(btn);
        root.setStyle("-fx-background-color: gray");
    
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.minWidthProperty().bind(scene.heightProperty().multiply(2));
        primaryStage.minHeightProperty().bind(scene.widthProperty().divide(2));
        primaryStage.show();
    }
    

提交回复
热议问题