簡體   English   中英

如何將此vbox在此邊框的中心區域內居中?

[英]How can I center this vbox within the center region of this 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();
}

這里的鍵是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();

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM