JavaFX 在一个场景中有多个窗格吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33339427/
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 Have multiple Panes in one scene?
提问by Hatefiend
I am trying to make an application which will have a date at the top (always automatically centered) and content at the bottom which is not going to be aligned to any direction.
我正在尝试制作一个应用程序,它的顶部有一个日期(总是自动居中),底部有一个不会与任何方向对齐的内容。
I figured the best way to do this would be to have:
我认为最好的方法是:
Pane normalLayout = new Pane();
StackPane centeredLayout = new Stackpane();
Label centeredText = new Label("I want this text centered!");
Button unorganizedButton = new Button("Press me");
centeredLayout.getChildren().add(centeredText);
normalLayout.getChildren().add(unorganizedButton);
But then I can't do something like:
但后来我不能做这样的事情:
Scene myScene = new Scene(centeredLayout, normalLayout, 500, 500);
Window myWindow = new Window();
myWindow.setScene(myScene);
myWindow.show();
So how can this be done? How can multiple panes exist on the same scene?
那么如何做到这一点呢?同一场景中如何存在多个窗格?
采纳答案by Marcel
The Scene it self can only have one rootPane. So if you want 2 panes in the Scene you need 3.
场景本身只能有一个根窗格。因此,如果您想要场景中的 2 个窗格,则需要 3 个。
Scene
|
V
Root Pane (Vbox for example)
| |
V V
Pane1 Pane2
In your code this can look like this:
在您的代码中,这可能如下所示:
StackPane rootPane = new StackPane();
Scene scene = new Scene(rootPane,...);
Pane pane1 = new Pane();
Pane pane2 = new Pane();
rootPane.getChildren().addAll(pane1,pane2);
Depending on how your Application should be layouted you have to choose the right Pane implementations.
根据您的应用程序的布局方式,您必须选择正确的窗格实现。
As a little Tip to get familiar whit all the Layout Containers try the SceneBuilder Application. http://gluonhq.com/open-source/scene-builder/
作为熟悉所有布局容器的小技巧,请尝试使用 SceneBuilder 应用程序。http://gluonhq.com/open-source/scene-builder/
Maybe this link will help you understanding how layouting works in JavaFX: http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htmhttps://docs.oracle.com/javafx/2/layout/builtin_layouts.htm
也许此链接将帮助您了解布局在 JavaFX 中的工作原理:http: //docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm https://docs.oracle.com/javafx/2/layout/内置布局.htm
回答by Yannick Rot
I would suggest you to create a "root"-Pane. In your case, you could use a BorderPane.
我建议您创建一个“根”窗格。在您的情况下,您可以使用 BorderPane。
Example:
例子:
BorderPane root = new BorderPane();
Label centeredText = new Label("I want this text centered!");
Button unorganizedButton = new Button("Press me");
BorderPane.setAlignment(centeredText, Pos.CENTER);
root.setTop(centeredText);
root.setBottom(unorganizedButton);
Afterwards just call the constructor with the newly created pane.
之后只需使用新创建的窗格调用构造函数。
Scene scene = new Scene(root, 500, 500);
Addition:
添加:
You could also just set new panes.
您也可以只设置新窗格。
AnchorPane anchorPane = new AnchorPane();
root.setTop(anchorPane);