![](/img/trans.png)
[英]Why JavaFX- method .getLayoutBounds().getWidth() returns 0?
[英]Cannot determine width of button because getWidth() returns 0 in JavaFx
我正在開發必須顯示樹的應用程序。 我開始於: JavaFX中的圖形可視化(如yFiles) 。 樹被顯示,但是對於ButtonCell調用方法getWidth或getBoundsInLocal的任何變體,getBoundsInParent,getLayoutBounds返回0,而getPrefWidth返回-1。
如何獲得按鈕的寬度和高度?
您必須在顯示階段之前添加組件,並在顯示階段之后獲取值,即:
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
graph = new Graph();
addGraphComponents();
root.setCenter(graph.getScrollPane());
Scene scene = new Scene(root, 1024, 768);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
Layout layout = new RandomLayout(graph);
layout.execute();
for( Cell cell: graph.getModel().getAllCells()) {
System.out.println(cell + ": " + cell.getBoundsInParent());
}
}
或者,您可以調用圖形創建后在Platform.runLater中進行的所有操作,即:
Platform.runLater(() -> {
for( Cell cell: graph.getModel().getAllCells()) {
System.out.println(cell + ": " + cell.getBoundsInParent());
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.