在JavaFX中调整Stage大小时强制布局更改

Bal*_*551 5 javafx

我在JavaFX中遇到了一个可能的错误,但是时间很短,我正在寻找一种解决方法.

我有一堆包含图表和工具栏的窗口:

public void createGui() {
    root = new VBox();

    ToolBar toolbar = new ToolBar();

    Button btnConfig = new Button(bundle.getString("chartWindow.menu.configure"));
    btnConfig.setOnAction(e -> doChartConfig());

    toolbar.getItems().addAll(btnConfig);

    chartPane = new VBox();
    root.getChildren().setAll(toolbar, chartPane);
    VBox.setVgrow(chartPane, Priority.ALWAYS);

    scene = new Scene(root);
    setScene(scene);

    updateChart();  // Creates a chart
}
Run Code Online (Sandbox Code Playgroud)

我必须从代​​码中调整它们的大小.(平铺打开的图表.)简化的平铺代码是:

    // bounds is the user available area of the monitors(s)
    double windowWidth = bounds.getWidth() / tileRule.columns;
    double windowHeight = bounds.getHeight() / tileRule.rows;

    int r = 0;
    int c = 0;
    for (Window w : sel) {
        w.setWidth(windowWidth);
        w.setHeight(windowHeight);
        w.setX(bounds.getMinX() + c * windowWidth);
        w.setY(bounds.getMinY() + r * windowHeight);
        c++;
        if (c >= tileRule.getColumns()) {
            c = 0;
            r++;
            if (r >= tileRule.rows) {
                break;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我这样做时,窗户完美排列:

在此输入图像描述

但是,由于它是可见的,一些窗口内容没有调整窗口大小(现在,不小心,它们是最后3个,但情况并非总是如此.有时会有更多,并且它们并不总是最后一个那些.)

可以清楚地看到,场景是未使用窗口调整大小的场景.

在此输入图像描述

一旦我手动调整窗口大小,控件就会很好地布局.

我把一堆东西搞砸了:

  • 手动调用requestLayout方法
  • 从场景中删除根元素并再次添加
  • 从舞台中删除场景并再次添加
  • 在调整大小操作之间添加100 ms延迟
  • 将根元素宽度绑定到窗口宽度(减去边框大小)

以上都没有帮助.(是的,甚至没有再添加场景!它引起了最引人注目的结果,因为内容被调整为窗口大小,但是通过拉伸其内容.)

有没有人有任何其他想法如何破解这个错误?

我正在使用Java 8u74.