Javafx将按钮对齐为菜单BorderPane

时间:2017-06-23 09:47:07

标签: java javafx

此时的按钮全部堆叠在场景的左下角。如何将它们移动到右侧,使它们像菜单一样保持对齐?

public class Mainfx extends Application { 
@Override
public void start(Stage primaryStage) { 
    Button btn1 = new Button("Botao 1"); 
    Button btn2 = new Button("Botao 2"); 
    Button btn3 = new Button("Botao 3"); 
    Button btn4 = new Button("Botao 4"); 
    Button btn5 = new Button("Botao 5");       
    BorderPane root = new BorderPane(); 
    root.setBottom(btn1); 
    root.setBottom(btn2); 
    root.setBottom(btn3); 
    root.setBottom(btn4); 
    root.setBottom(btn5); 
    primaryStage.setTitle("Border Layout"); 
    primaryStage.setScene(new Scene(root, 300, 250)); 
    primaryStage.show(); 
} 
public static void main(String[] args) { 
    launch(args); 
} 
}

1 个答案:

答案 0 :(得分:2)

将按钮放在HBox容器中可以获得所需的结果。然后将HBox放在BorderPane中。

public class Main extends Application {


    @Override
    public void start(Stage primaryStage) {

        Button btn1 = new Button("Botao 1");
        Button btn2 = new Button("Botao 2");
        Button btn3 = new Button("Botao 3");
        Button btn4 = new Button("Botao 4");
        Button btn5 = new Button("Botao 5");

        HBox hbox = new HBox();
        hbox.getChildren().addAll(btn1, btn2, btn3, btn4, btn5);

        BorderPane root = new BorderPane(); 
        root.setBottom(hbox);

        primaryStage.setTitle("Border Layout");
        primaryStage.setScene(new Scene(root, 450, 250));
        primaryStage.show();
    }


    public static void main(String[] args) {
         launch(args);
    }

}