如何使JavaFX舞台仅在高度上可调整大小?

huangapple go评论71阅读模式
英文:

How to make javafx stage resizable only in height?

问题

我有一个舞台,我想让用户只能更改其高度,而不能更改宽度。我找到的唯一解决方案是stage.setResizable(false);,但它也不允许更改高度。如何做到这一点?

英文:

I have a stage and I want to let user change only its height, but not the width. The only solution I found is stage.setResizable(false); but it also doesn't allow to change height. How to do it?

答案1

得分: 5

你可以使用以下代码设置固定宽度:

stage.setMaxWidth(x);
stage.setMinWidth(x);

如果你将舞台设置为可调整大小,那么你只能在高度方向上进行调整。

英文:

You could set a fixed width with:

stage.setMaxWidth(x);
stage.setMinWidth(x);

If you then set the stage to resizable, you should only be able to resize it in its height.

答案2

得分: 0

更好的做法是采用更加通用的方法,特别是如果您的程序正在使用FXML。为此,我们可以将最小宽度和最大宽度绑定到自身,从而不可由用户调整。

stage.minWidthProperty().bind(stage.widthProperty());
stage.maxWidthProperty().bind(stage.widthProperty());
英文:

It is better to have a more versatile approach especially if your program is using FXML. To do that we can bind the min and max width to be fixed to itself, thus non-adjustable by the user.

stage.minWidthProperty().bind(stage.widthProperty());
stage.maxWidthProperty().bind(stage.widthProperty());

huangapple
  • 本文由 发表于 2020年9月13日 16:06:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定