如何将此vbox置于此边框的中心区域中?

时间:2018-04-06 19:57:30

标签: java javafx vbox borderpane

我正在尝试创建游戏的第一个窗口,并创建一个放置在第一个屏幕中心的菜单。

    BorderPane bp = new BorderPane();
Button Start = new Button("Start");
Button Mute = new Button("Mute");
Button Exit = new Button("Quit");
Button Options = new Button("Options");

/*
 * Vbox in Center for buttons:
 * (non-Javadoc)
 * @see javafx.application.Application#start(javafx.stage.Stage)
 */

public void start(Stage primaryStage) {

    VBox vb = new VBox(100);
    vb.getChildren().addAll(Start,Options,Mute,Exit);
    vb.setMargin(Start, new Insets(0,0,0,15));
    vb.setMargin(Options, new Insets(0,0,0,15));
    vb.setMargin(Mute, new Insets(0,0,0,15));
    vb.setMargin(Exit, new Insets(0,0,0,15));
    bp.setCenter(vb);

    Scene scene = new Scene(bp, WIDTH, HEIGHT);
    primaryStage.setTitle("Tetris");
    primaryStage.setScene(scene);
    primaryStage.show();
}

1 个答案:

答案 0 :(得分:0)

这里的关键是vb.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);。我在VBox上设置了背景颜色,以便您可以更好地了解情况。

    BorderPane bp = new BorderPane();
    Button Start = new Button("Start");
    Button Mute = new Button("Mute");
    Button Exit = new Button("Quit");
    Button Options = new Button("Options");

    VBox vb = new VBox(100);
    vb.setStyle("-fx-background-color: yellow");
    vb.getChildren().addAll(Start,Options,Mute,Exit);
    vb.setMargin(Start, new Insets(0,0,0,15));
    vb.setMargin(Options, new Insets(0,0,0,15));
    vb.setMargin(Mute, new Insets(0,0,0,15));
    vb.setMargin(Exit, new Insets(0,0,0,15));
    vb.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);
    bp.setCenter(vb);

    Scene scene = new Scene(bp, 500, 720);
    primaryStage.setTitle("Tetris");
    primaryStage.setScene(scene);
    primaryStage.show();

enter image description here