我一直在试图设置场景的宽度和高度以外的构造函数,它是无济于事。在看完Scene
API后,我看到了一种方法,可以分别获取高度和宽度,但不能设置方法..:s(设计缺陷也许)。设置场景的宽度和高度
经过进一步的研究,我遇到了SceneBuilder
,并找到了可以修改高度和宽度的方法。但是,我不知道如何将其应用于已创建的场景对象,或者如何创建可用于替代场景对象的SceneBuilder
对象。
我一直在试图设置场景的宽度和高度以外的构造函数,它是无济于事。在看完Scene
API后,我看到了一种方法,可以分别获取高度和宽度,但不能设置方法..:s(设计缺陷也许)。设置场景的宽度和高度
经过进一步的研究,我遇到了SceneBuilder
,并找到了可以修改高度和宽度的方法。但是,我不知道如何将其应用于已创建的场景对象,或者如何创建可用于替代场景对象的SceneBuilder
对象。
一旦您创建了Scene
并将其指定给Stage
,则可以使用Stage.setWidth
和Stage.setHeight
同时更改舞台和场景尺寸。
SceneBuilder
不能应用于已经创建的对象,它只能用于场景创建。
我只是想为那些可能与我有类似问题的人发表另一个答案。
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Scene.html
没有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
,正如你所描述的那样,并且需要它的宽度和高度。我正在创建自定义控件,所以很奇怪它并不会自动执行,所以这是如何做到的,如果你需要的话。
我本可以使用Stage
的setWidth()
/setHeight()
。
似乎无法在创建后设置Scene
的大小。
设置Stage
的大小意味着设置窗口的大小,其中包括装饰的大小。所以Scene
的尺寸较小,除非Stage
未修饰。
我的解决方法是计算而初始化装修的大小,并将其添加到Stage
的大小调整时:
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);
}
好的,谢谢。这是我没有想过使用或不知道我可以使用的途径。非常感谢,我会尝试一下,让你知道它是如何去的。 – Chika 2012-01-10 09:22:38
非常感谢。它的作品你的建议是完美的,但还有一个问题。我应该能够清除场景或舞台?我怎样才能做到这一点? – Chika 2012-01-10 14:05:39
为“清除”阶段分配一个空的场景:'stage.setScene(new Scene());'。要清除场景,请设置一个空根:'scene.setRoot(new Group());'或者删除所有根子节点:'scene.getRoot()。getChildren()。clear();' – 2012-01-10 14:18:49