英文:
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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论