0

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

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

1 回答 1

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

在此处输入图像描述

于 2018-04-07T15:39:42.757 回答