JavaFX:如何根据舞台将中心节点置于边框的中央?

JavaFX: How to center the center node on a borderpane according to the Stage?

我在根据舞台居中边框窗格中的按钮时遇到问题。它根据 borderpane 居中,但我希望它位于 window.

的正中心

我指的是我的图片底部有播放按钮和难度选择框。

我正在使用边框让难度部分在右侧,播放按钮在中间。

正如你所看到的,播放没有在图片的中心。

我想这样做,但我不确定怎么做。我认为我可以将左节点的宽度设置为等于右节点的宽度,但我的左节点没有任何东西。

// bottom section for positioning
    BorderPane settings = new BorderPane();
    settings.setPadding(new Insets(10));

    // play button
    Button playBtn = new Button("Play");
    settings.setCenter(playBtn);

    // difficulty options
    Label diffLabel = new Label("Difficulty: ");
    ChoiceBox<String> diffBox = new ChoiceBox<String>(FXCollections.observableArrayList(
            "Easy", "Standard", "Hard"
    ));
    diffBox.getSelectionModel().select(1);

    // for wrapping the diffuclty.
    HBox difficulty = new HBox(diffLabel, diffBox);
    difficulty.setAlignment(Pos.CENTER);
    difficulty.setSpacing(10);

    settings.setRight(difficulty);

其中一种可能性是在 BorderPane 的左侧添加一个 Region 以起到垫片的作用。

来自doc of BorderPane

The left and right children will be resized to their preferred widths and extend the length between the top and bottom nodes. And the center node will be resized to fill the available space in the middle.

因此在右侧添加一个与HBox宽度相同的垫片应该可以解决问题:

Region padderRegion = new Region();
padderRegion.prefWidthProperty().bind(difficulty.widthProperty());
settings.setLeft(padderRegion);

此处左侧间隔符的宽度绑定到右侧 HBox 的宽度,因此居中的 Node(播放按钮)将在屏幕上居中。

另一种可能性是在 HBox:

中使用各种 padders
HBox bottomPanel = new HBox();
bottomPanel.setAlignment(Pos.CENTER);

Region padderRegion1 = new Region();
Region padderRegion2 = new Region();
Region padderRegion3 = new Region();
padderRegion1.prefWidthProperty().bind(diffLabel.widthProperty().add(diffBox.widthProperty()));

bottomPanel.getChildren().addAll(padderRegion1, padderRegion2, playBtn, padderRegion3, diffLabel, diffBox);
HBox.setHgrow(padderRegion2, Priority.ALWAYS);
HBox.setHgrow(padderRegion3, Priority.ALWAYS);

试试这个:

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    public void start(Stage stage) {

        BorderPane settings = new BorderPane();
        settings.setPadding(new Insets(10));

        Button playBtn = new Button("Play");

        Label zombieLabel = new Label("Zombie Dice");
        zombieLabel.setStyle("-fx-font-size: 60");
        zombieLabel.setAlignment(Pos.CENTER);
        HBox zomBox = new HBox();
        zomBox.getChildren().add(zombieLabel);
        zomBox.setAlignment(Pos.CENTER);

        Label indicateLabel = new Label("Indicate who is playing");
        indicateLabel.setStyle("-fx-font-size: 20");
        indicateLabel.setAlignment(Pos.CENTER);
        HBox indBox = new HBox();
        indBox.getChildren().add(indicateLabel);
        indBox.setAlignment(Pos.CENTER);


        ChoiceBox<String> diffBox = new ChoiceBox<>(FXCollections.observableArrayList(
                "Easy", "Standard", "Hard"
        ));
        diffBox.getSelectionModel().select(1);

        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(5));
        gridPane.setHgap(5);
        gridPane.setVgap(5);

        ColumnConstraints col1 = new ColumnConstraints(150);
        ColumnConstraints col2 = new ColumnConstraints(150);
        ColumnConstraints col3 = new ColumnConstraints(150);

        gridPane.getColumnConstraints().addAll(col1, col2, col3);
        gridPane.setAlignment(Pos.CENTER);

        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(playBtn);
        hBox.setAlignment(Pos.CENTER);

        gridPane.add(zomBox, 0, 0, 3, 1);
        gridPane.add(indBox, 0, 1, 3, 1);
        gridPane.add(hBox, 1, 2);
        gridPane.add(diffBox, 2, 2);

        Scene root = new Scene(gridPane);
        stage.setScene(root);
        stage.show();
    }
}

可能你必须玩一下 padding 和 gap。