[英]Javafx aligning buttons as a menu BorderPane
The buttons at this moment are all stacked in the left bottom corner of the scene. 此时所有按钮都堆叠在场景的左下角。 How can I move them to the right side so that they stay all aligned like a menu? 如何将它们移到右侧,以便它们像菜单一样保持对齐?
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);
}
}
Placing the buttons in an HBox container will give you the desired result. 将按钮放在HBox容器中将给您所需的结果。 Then place the HBox within the BorderPane. 然后将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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.