2012-12-12 120 views
0

我想我的對齊按鈕,這樣第一個按鈕在屏幕的左側,另外兩個在右邊。我目前正在使用HBox試圖定位它們,但我似乎無法弄清楚如何正確佈置它們。下面的代碼是我現在使用的。在JavaFX佈局對齊按鈕

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Priority; 
import javafx.stage.Stage; 

public class ButtonTest extends Application { 
    private Button min, close, openfile; 
    public static void main(String[] args){ 
     launch(args); 
    } 

    @Override 
    public void start(final Stage stage) throws Exception { 
     stage.setTitle("Button Test"); 
     Group root = new Group(); 
     BorderPane borderpane = new BorderPane(); 
     setUpButtons(); 
     HBox hbox = new HBox(); 
     hbox.setSpacing(10); 
     hbox.getChildren().add(openfile); 
     HBox hbox1 = new HBox(); 
     hbox1.setAlignment(Pos.CENTER_RIGHT); 
     hbox1.getChildren().addAll(min, close); 

     hbox.getChildren().add(hbox1); 
     HBox.setHgrow(hbox1, Priority.ALWAYS); 
     borderpane.setTop(hbox); 
     root.getChildren().add(borderpane); 
     Scene scene = new Scene(root,800,600); 
     stage.setFullScreen(true); 
     scene.getStylesheets().add("button.css"); 
     stage.setScene(scene); 
     stage.show(); 

    } 

    private void setUpButtons() { 
     close = new Button("x"); 
     close.setId("closeBtn"); 

     min = new Button("_"); 
     min.setId("minBtn"); 

     openfile = new Button("Open file"); 
     openfile.setId("openFileBtn"); 


    } 

} 

任何幫助,將不勝感激 感謝

回答

3

設置你的外HBox中(橫向盒),以LEFT的aligment,和你內心的HBox中(hbox1)到右的aligment。

然後你可以在右邊的左外的內容,內部內容。

*編輯:現在得到你的問題。停止使用組直接添加borderpane的場景:

Scene scene = new Scene(borderpane,800,600);