简体   繁体   English

如何将此vbox在此边框的中心区域内居中?

[英]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); 这里的键是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. 我在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();

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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