问题
Is it possible to create a Stage in JavaFx whose height changes dynamically? Or, in other words, is it possible to change the height of a Stage once it has been created (stage.show()
), so that the stage morph's into its new height?
回答1:
Use stage.sizeToScene() to resize the stage to whatever the current preferred size of it's root is.
stage.setScene(new Scene(someContent));
stage.show();
....
stage.getScene().setRoot(someNewContent));
stage.sizeToScene();
You can also just call stage.setWidth() or stage.setHeight() if you don't need the automatic sizing behavior of sizeToScene()
.
You can use a Timeline to animate the stage size change if you need to do that.
来源:https://stackoverflow.com/questions/31360919/how-to-dynamically-change-the-height-of-a-stage-in-javafx