Change Scene without resize window in JavaFX

后端 未结 2 878
广开言路
广开言路 2020-12-21 10:52

I\'m trying to change Scenes on JavaFX, but without change the window size. However, when I set stage.setScene(scene2); the window size decreases, and I want to

2条回答
  •  天涯浪人
    2020-12-21 11:10

    It's probably better here just to replace the root of the existing scene, than to create a new scene:

    fadeOut.setOnFinished((e) -> {
        try {               
            Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml"));
    
            scene.setRoot(root2);
    
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    });
    

    If you really do need to replace the scene, for some reason, you can set the new scene's size to the same as the existing scene:

    fadeOut.setOnFinished((e) -> {
        try {               
            Parent root2 = FXMLLoader.load(getClass().getResource("../view/fxml/Welcome.fxml"));
    
            Scene scene2 = new Scene(root2, scene.getWidth(), scene.getHeight());
    
            stage.setScene(scene2);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    });
    

提交回复
热议问题