4

I am having a problem centering a button in my borderpane to be centered according to the stage. It is centered according to the borderpane but I want it to be in the exact center of the window.

enter image description here

I am referring to the bottom part of my picture which has the play button and difficulty choiceBox.

I am using a borderpane to have the difficulty part be on the right side and the play button in the center.

As you can see, play is not centered in the picture.

I would like to do that but I'm not sure how. I think that I could possibly set the width of my left node to be equal to my right node but I don't have anything for my left Node.

// 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);
4

2 回答 2

3

其中一种可能性是Region在左侧添加一个BorderPane作为间隔符。

来自以下文档BorderPane

左右子节点将被调整为其首选宽度并延长顶部和底部节点之间的长度。并且中心节点将调整大小以填充中间的可用空间。

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

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

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

另一种可能性是在 a 中使用各种填充器HBox

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);
于 2016-11-03T07:02:19.573 回答
0

尝试这个:

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

可能您必须使用填充和间隙。

于 2016-11-03T07:29:24.417 回答