[英]How to listen resize event of Stage in JavaFX?
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 中检测表单的调整大小事件?
You can listen to the changes of the widthProperty
and the heightProperty
of the Stage
:您可以监听
Stage
的widthProperty
和heightProperty
的变化:
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);
Keeping a fixed width to height ratio:保持固定的宽高比:
stage.minHeightProperty().bind(stage.widthProperty().multiply(0.5));
stage.maxHeightProperty().bind(stage.widthProperty().multiply(0.5));
Cut the long story short :长话短说:
container.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (newValue.floatValue()!=oldValue.floatValue()) resizeKids(newValue);
}
});
Also you may want to check new width/height with the old values for prevent duplication .此外,您可能希望使用旧值检查新的宽度/高度以防止重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.