2017-01-23 9 views
2

2つのコンポーネント:Label & Buttonがあります。私はそれらを並べて、&を一緒にCENTREに整列させたいと思っています。しかし、私はそうしていませんでした。彼らはまだLEFTではなくCENTERとは一線を画しています。JavaFX複数のコンポーネントを並べる方法

enter image description here

次のように私のコード:

Label sloganLbl = new Label("With cost as low as $1.99, you can own a fraction share of U.S. stock. No account yet?"); 
sloganLbl.getStyleClass().add("blue-small-font"); 


Button signUpBtn = new Button("Open account now"); 
signUpBtn.getStyleClass().add("green-btn-small-font"); 

GridPane topGrid = new GridPane(); 

topGrid.setHgap(20); 
topGrid.add(sloganLbl, 0, 0); 
topGrid.add(signUpBtn, 1, 0); 
topGrid.setGridLinesVisible(true); 

GridPane.setHalignment(sloganLbl, HPos.RIGHT); 
GridPane.setHalignment(signUpBtn, HPos.LEFT); 

BorderPane topBorder = new BorderPane(); 
topBorder.setPadding(new Insets(15, 10, 15, 10)); 
topBorder.setCenter(topGrid); 

topBorder.getStyleClass().add("blue-small-font"); 
topGrid.getStyleClass().add("blue-small-font"); 

borderPane.setTop(topBorder); 

問題がtopBorder.setCenter(topGrid);では、中心にあるコンテンツをセンタリングすることができません。 topGridは2列の合計幅だけでなく、全幅を占めているようです。

どのようにセンターアライメントを達成できますか?ありがとう!

答えて

2

GridPaneは、左側と右側のギャップフィラー列で更新できます。

ColumnConstraints leftFiller = new ColumnConstraints(); 
leftFiller.setHgrow(Priority.ALWAYS); // Fills the space left on the left side 
ColumnConstraints rightFiller = new ColumnConstraints(); 
rightFiller.setHgrow(Priority.ALWAYS); // Fills the space left on the right side 
topGrid.getColumnConstraints().addAll(leftFiller, new ColumnConstraints(), 
    new ColumnConstraints(), rightFiller); 

topGrid.add(sloganLbl, 1, 0); 
topGrid.add(signUpBtn, 2, 0); 

このスニペットはGridPane 4との列を作成します。 GridPaneがサイズ変更されると、最初と最後が大きくなり、2つの内側の列はLabelButtonを保持します。

LabelButtonが2番目と3番目の列に配置されていることに注意してください。

+0

非常に涼しい@DVarga、あなたのコードは私の問題を解決しました。本当にありがとう! –

関連する問題