我正在處理需要顯示樹的應用程序。我從:Graph Visualisation (like yFiles) in JavaFX開始。顯示樹,但對於ButtonCell調用方法getWidth或getBoundsInLocal,getBoundsInParent,getLayoutBounds的任何變體返回0,getPrefWidth返回-1。無法確定按鈕的寬度,因爲getWidth()在JavaFx中返回0
如何獲得按鈕的寬度和高度?
我正在處理需要顯示樹的應用程序。我從:Graph Visualisation (like yFiles) in JavaFX開始。顯示樹,但對於ButtonCell調用方法getWidth或getBoundsInLocal,getBoundsInParent,getLayoutBounds的任何變體返回0,getPrefWidth返回-1。無法確定按鈕的寬度,因爲getWidth()在JavaFx中返回0
如何獲得按鈕的寬度和高度?
您必須在顯示階段之前添加組件,並在顯示階段後獲取值。 Ë:
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,我以後不管發生什麼事。 e .:
Platform.runLater(() -> {
for(Cell cell: graph.getModel().getAllCells()) {
System.out.println(cell + ": " + cell.getBoundsInParent());
}
});
謝謝你的回答。我試過了,後一個版本與Platform.runLater一起工作,而前者只有minX/minY的非零值。 – Andrew
你什麼時候調用這些方法?如果在佈局執行之前調用它們(通常只發生在UI實際呈現到屏幕上時),它們將給出尺寸爲零。 (這可能會幫助發佈一個[MCVE]。) –
看起來像這可能是重複的:[獲取JavaFX節點的高度(生成佈局傳遞)](http://stackoverflow.com/questions/ 26152642 /獲取最高度對的一節點功能於JavaFX的生成-A-佈局通)。 – jewelsea