如何在 JavaFX 中监听 Stage 的调整大小事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38216268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to listen resize event of Stage in JavaFX?
提问by Amita Patil
I want to perform some functionality on resize event of form (or Scene
or Stage
whatever it is).
我想对表单(Scene
或Stage
其他任何内容)的调整大小事件执行一些功能。
But how can I detect resize event of form in JavaFX?
但是如何在 JavaFX 中检测表单的调整大小事件?
采纳答案by DVarga
You can listen to the changes of the widthProperty
and the heightProperty
of the Stage
:
你可以听的变化widthProperty
及heightProperty
的Stage
:
stage.widthProperty().addListener((obs, oldVal, newVal) -> {
// Do whatever you want
});
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
// Do whatever you want
});
Note:To listen to both width and height changes, the same listener can be used really simply:
注意:要监听宽度和高度的变化,可以非常简单地使用同一个监听器:
ChangeListener<Number> stageSizeListener = (observable, oldValue, newValue) ->
System.out.println("Height: " + stage.getHeight() + " Width: " + stage.getWidth());
stage.widthProperty().addListener(stageSizeListener);
stage.heightProperty().addListener(stageSizeListener);
回答by M. S.
Keeping a fixed width to height ratio:
保持固定的宽高比:
stage.minHeightProperty().bind(stage.widthProperty().multiply(0.5));
stage.maxHeightProperty().bind(stage.widthProperty().multiply(0.5));