简体   繁体   English

JavaFX:如何根据舞台将中心节点放在边框上?

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

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. 它根据边界窗格居中,但我希望它位于窗口的确切中心。

在此处输入图片说明

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

One of the possibilities is to add a Region to the left side of the BorderPane to act like a spacer. 一种可能性是将一个Region添加到BorderPane以充当间隔符。

From the doc of BorderPane : 来自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. 中心节点将被调整大小以填充中间的可用空间。

Therefore adding a spacer with the same width as the HBox on the right side should solve the problem: 因此,添加与右侧HBox宽度相同的垫片可以解决此问题:

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

Here the width of the left spacer is bound to the width of the HBox on the right side, therefore the centered Node (the play button) will be centered on the screen. 在这里,左间隔子的宽度与右侧HBox的宽度绑定在一起,因此居中的Node (播放按钮)将在屏幕上居中。

Another possibility is to use various padders within a HBox : 另一种可能性是在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);

try this: 尝试这个:

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

probably you have to play with padding and gap. 可能您必须使用填充和间隙。

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

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