簡體   English   中英

JavaFX:如何根據舞台將中心節點放在邊框上?

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

我在將邊框中的按鈕居中以根據舞台居中時遇到問題。 它根據邊界窗格居中,但我希望它位於窗口的確切中心。

在此處輸入圖片說明

我指的是圖片的底部,其中包含播放按鈕和難度選擇框。

我使用的是邊框,難度部分在右側,播放按鈕在中間。

如您所見,播放不在圖片中居中。

我想這樣做,但不確定如何。 我認為我可以將左節點的寬度設置為等於右節點的寬度,但是左節點沒有任何東西。

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

一種可能性是將一個Region添加到BorderPane以充當間隔符。

來自BorderPane文檔

左側和右側子級將調整為其首選寬度,並在頂部和底部節點之間延長長度。 中心節點將被調整大小以填充中間的可用空間。

因此,添加與右側HBox寬度相同的墊片可以解決此問題:

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

在這里,左間隔子的寬度與右側HBox的寬度綁定在一起,因此居中的Node (播放按鈕)將在屏幕上居中。

另一種可能性是在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);

嘗試這個:

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

可能您必須使用填充和間隙。

暫無
暫無

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

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