2016-04-26 53 views
1

我想將結果設置爲窗格的中心。如何在javafx中的窗格上居中放置標籤

Label results = new Label("You win"); 
Pane pane = new Pane(); 
pane.getChildren().add(results); 

我已經嘗試了下面的兩行代碼,但都不起作用。

results.setAlignment(Pos.CENTER); 
results.setContentDisplay(ContentDisplay.CENTER); 

順便說一句,我知道如何得到這個中心與StackPane和GridPane但在這種情況下,我需要使用一個普通的窗格。

回答

1

Pane不支持居中兒童。因此必須手動完成,即layoutXlayoutY位置必須「手動」計算。

您可以通過綁定實現這一目標:

results.layoutXProperty().bind(pane.widthProperty().subtract(results.widthProperty()).divide(2)); 
// procede accordingly with layoutY/height 
+0

IDK如何工作的,但它確實!非常感謝 –