【问题标题】:How can I change the size of a StackPane that is inside of a VBox, and allow the VBox to stretch to that size?如何更改 VBox 内的 StackPane 的大小,并允许 VBox 拉伸到该大小?
【发布时间】: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


    【解决方案1】:

    我找到了一个不理想但有效的变通解决方案。我创建了一个透明矩形并将其放置在 StackPane 的后面,并将 VBox、场景和舞台调整为矩形的大小。

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2013-09-09
      • 2015-04-10
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多