java sizeToScene 调用后的 JavaFX 舞台宽度/高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17265493/
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
JavaFX Stage width/height after sizeToScene call
提问by Morv
I'm currently writing an application that I want to have a minimum scene size of 800x600 pixels.
At first attempt I simply used stage.setMinWidthand stage.setMinHeightbut these values count for the whole window and not only for the scene, so only 7xx x 5xx of the scene are shown.
So I searched the Stage javadoc and found sizeToScenewhich is exactly what i need, after call the stage size is bigger than 800x600 and the scene is fully shown with it's 800x600 pixels.
我目前正在编写一个应用程序,我希望最小场景尺寸为 800x600 像素。第一次尝试时,我只是简单地使用了stage.setMinWidthandstage.setMinHeight但这些值对整个窗口计数,而不仅仅是对场景,所以只显示了 7xx x 5xx 的场景。所以我搜索了舞台 javadoc 并发现sizeToScene这正是我需要的,在调用后舞台尺寸大于 800x600 并且场景完全显示为 800x600 像素。
The only problem now is that this method call doesn't save the stage width and height in the stage itself and therefore I can't set the minimum width and height to the current width and height calculated by sizeToScene.
现在唯一的问题是此方法调用不会在舞台本身中保存舞台宽度和高度,因此我无法将最小宽度和高度设置为由sizeToScene.
stage.getWidthand stage.getHeightalways return NaN.
stage.getWidth并且stage.getHeight总是返回 NaN。
I could hardcode these values now that I got them (made a screenshot and checked in paint.net) but this wouldn't work with other OS/versions of Windows/decorators.
我现在可以对这些值进行硬编码(制作屏幕截图并在 Paint.net 中检查),但这不适用于其他操作系统/版本的 Windows/装饰器。
I'm working on Windows 7, JavaFX 2.2, JDK 7_21.
我正在使用 Windows 7、JavaFX 2.2、JDK 7_21。
Has someone got an idea how to get the width and height after the method call? I hope you understand what my problem is otherwise just tell me. If you need code i can give it to you but i guess you won't need it. Thanks!
有人知道如何在方法调用后获取宽度和高度吗?我希望你明白我的问题是什么,否则就告诉我。如果您需要代码,我可以给您,但我想您不需要它。谢谢!
回答by Uluk Biy
The stage's width and height values are calculated after the stage has been shown. You can set minimum values of these values after the stage shown:
舞台的宽度和高度值是在舞台显示后计算的。您可以在显示的阶段之后设置这些值的最小值:
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
System.out.println("before sceneW " + scene.getWidth());
System.out.println("before sceneH " + scene.getHeight());
System.out.println("before stageW " + primaryStage.getWidth());
System.out.println("before stageH " + primaryStage.getHeight());
primaryStage.show();
System.out.println("after sceneW " + scene.getWidth());
System.out.println("after sceneH " + scene.getHeight());
System.out.println("after stageW " + primaryStage.getWidth());
System.out.println("after stageH " + primaryStage.getHeight());
primaryStage.setMinWidth(primaryStage.getWidth());
primaryStage.setMinHeight(primaryStage.getHeight());
}
sizeToScene()is similar to Swing's (AWT) pack(). I think you don't need it here.
sizeToScene()类似于 Swing 的 (AWT) pack()。我认为你在这里不需要它。

