如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:06:03  来源:igfitidea点击:

How to listen resize event of Stage in JavaFX?

javajavafxpropertiesjavafx-8listener

提问by Amita Patil

I want to perform some functionality on resize event of form (or Sceneor Stagewhatever it is).

我想对表单(SceneStage其他任何内容)的调整大小事件执行一些功能。

But how can I detect resize event of form in JavaFX?

但是如何在 JavaFX 中检测表单的调整大小事件?

采纳答案by DVarga

You can listen to the changes of the widthPropertyand the heightPropertyof the Stage:

你可以听的变化widthPropertyheightPropertyStage

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));