繁体   English   中英

为什么 JavaFX- 方法 .getLayoutBounds().getWidth() 返回 0?

[英]Why JavaFX- method .getLayoutBounds().getWidth() returns 0?

我需要动态检索JavaFX-BorderPane左侧部分的宽度 这是代码片段,但我使用的两种方法总是返回 0,尽管此时已布置左组件......

double leftWidth = ((VBox) this.getLeft()).getWidth();

与:

double leftWidth = ((VBox) this.getLeft()).getLayoutBounds().getWidth();

如何正确地做到这一点?

这里有一个 mcve:

public class BorderPaneExample extends Application {

@Override
public void start(Stage primaryStage) {

    Button btn = new Button();
    btn.setText("Left side of BorderPane");
    BorderPane root = new BorderPane();
    root.setLeft(btn);
    Scene scene = new Scene(root, 640, 480);
    primaryStage.setScene(scene);

    primaryStage.show();

    // ... later on retrieve the width of the left side:
    System.out.println(root.getLeft().getLayoutBounds().getWidth());
}

public static void main(String[] args) {
    launch(args);
}}

很可能您是在stage.show()发生之前调用它们的。

在显示窗口之前不会初始化边界。 将它们移动到稍后计算,或者尝试将它们包装到Platform.runLater()作为解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM