简体   繁体   中英

How can I center this vbox within the center region of this borderpane?

I'm trying to create the first window of a game, and creating a menu to be placed in the center of this first screen.

    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();
}

The key here is vb.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE); . I set a background color on the VBox so that you can see what's happening better.

    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();

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM