Javafx 2 设置场景宽度和高度
我一直试图在构造器之外设置场景的宽度和高度,但没有效果。在浏览了Javafx 2 设置场景宽度和高度,javafx-2,scenebuilder,Javafx 2,Scenebuilder,我一直试图在构造器之外设置场景的宽度和高度,但没有效果。在浏览了场景API之后,我看到了一个方法,该方法可以分别获得高度和宽度,但不能设置方法..:s(可能是设计缺陷) 经过进一步的研究,我发现了SceneBuilder,并找到了可以修改高度和宽度的方法。然而,我不知道如何将其应用于已创建的场景对象,或者如何创建可用于替代场景对象的SceneBuilder对象。一旦创建了scene,并将其分配给Stage,您可以使用Stage.setWidth和Stage.setHeight来更改Stage和同
场景
API之后,我看到了一个方法,该方法可以分别获得高度和宽度,但不能设置方法..:s(可能是设计缺陷)
经过进一步的研究,我发现了
SceneBuilder
,并找到了可以修改高度和宽度的方法。然而,我不知道如何将其应用于已创建的场景对象,或者如何创建可用于替代场景对象的SceneBuilder
对象。一旦创建了scene
,并将其分配给Stage
,您可以使用Stage.setWidth
和Stage.setHeight
来更改Stage和同时调整场景大小
SceneBuilder
无法应用于已创建的对象,它只能用于场景创建。我只想为那些可能有类似问题的人发布另一个答案
没有setWidth()
或setHeight()
,属性为ReadOnly
,但是如果您查看
Constructors
Scene(Parent root)
Creates a Scene for a specific root Node.
Scene(Parent root, double width, double height)
Creates a Scene for a specific root Node with a specific size.
Scene(Parent root, double width, double height, boolean depthBuffer)
Constructs a scene consisting of a root, with a dimension of width and height, and specifies whether a depth buffer is created for this scene.
Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialiasing antiAliasing)
Constructs a scene consisting of a root, with a dimension of width and height, specifies whether a depth buffer is created for this scene and specifies whether scene anti-aliasing is requested.
Scene(Parent root, double width, double height, Paint fill)
Creates a Scene for a specific root Node with a specific size and fill.
Scene(Parent root, Paint fill)
Creates a Scene for a specific root Node with a fill.
如您所见,如果需要,您可以在此处设置高度和宽度
对我来说,我使用的是SceneBuilder
,正如您所描述的,我需要它的宽度和高度。我正在创建自定义控件,所以很奇怪它没有自动执行,所以如果需要,这就是如何执行的
我也可以从
阶段使用setWidth()
/setHeight()
。在创建场景后,似乎无法设置其大小
设置阶段的大小
意味着设置窗口的大小,其中包括装饰的大小。因此场景
的大小较小,除非舞台
未装饰
我的解决方案是在初始化时计算装饰的大小,并在调整大小时将其添加到阶段的大小中:
private Stage stage;
private double decorationWidth;
private double decorationHeight;
public void start(Stage stage) throws Exception {
this.stage = stage;
final double initialSceneWidth = 720;
final double initialSceneHeight = 640;
final Parent root = createRoot();
final Scene scene = new Scene(root, initialSceneWidth, initialSceneHeight);
this.stage.setScene(scene);
this.stage.show();
this.decorationWidth = initialSceneWidth - scene.getWidth();
this.decorationHeight = initialSceneHeight - scene.getHeight();
}
public void resizeScene(double width, double height) {
this.stage.setWidth(width + this.decorationWidth);
this.stage.setHeight(height + this.decorationHeight);
}
我认为自从这个答案被写出来后,API已经有了一些变化Scene.getX()
现在总是等于initialSceneX
,所以这个答案总是给出0的装饰大小。为了让它正常工作,我必须将装饰计算更改为stage.getWidth()-initialWidth
和stage.getHeight()-initialHeight
。但在那之后,它的工作完美无瑕!这正是我想要的智能解决方案,谢谢!请注意,此答案与设置场景宽度或高度不同,因为Stage.setWidth()
和Stage.setHeight
包括窗口的装饰。如果这对您很重要,请参阅下面@user2229691的答案。