【发布时间】:2017-09-02 03:59:06
【问题描述】:
我有一个 StackPane,其中的节点会根据 StackPane 的当前大小自行调整大小。我将这个 StackPane 放置在 VBox 中,它是场景的根。 StackPane 根据场景的大小增长和缩小(即当用户调整窗口大小时增长和缩小)。所有这些功能都有效,但是当程序运行时,程序创建的窗口非常小,用户必须将其调整为更大才能看到其中的元素。
我尝试设置 StackPane 的 prefWidth 和 prefHeight,但 VBox 似乎没有根据 StackPane 的 prefWidth 和 prefHeight 调整自身大小。我还考虑过设置 VBox 的 prefWidth 和 prefHeight,这可行,但并不理想,因为我最终不得不确定所有节点的大小,而不是让它们计算自己的大小。
我所拥有的简单等价物:
VBox root = new VBox();
MenuBar menu = new MenuBar();
Rectangle rect = new Rectangle(250, 50);
StackPane sp = new StackPane();
//sp.setPrefSize(300, 300);
root.getChildren().addAll(menu, rect, sp);
Scene scene = new Scene(root);
sp.prefWidthProperty().bind(scene.widthProperty());
sp.prefHeightProperty().bind(scene.heightProperty().subtract(menu.heightProperty().subtract(rect.heightProperty()));
primaryStage.setScene(scene);
primaryStage.show();
这样做的聪明方法是什么?
【问题讨论】:
标签: java user-interface javafx javafx-8