4

ボーダーペインのボタンをステージに合わせて中央に配置するのに問題があります。ボーダーペインに従って中央に配置されていますが、ウィンドウの正確な中央に配置したいです。

ここに画像の説明を入力

再生ボタンと難易度選択ボックスがある私の写真の下部を参照しています。

ボーダーペインを使用して、難しい部分を右側に、再生ボタンを中央に配置しています。

ご覧のとおり、遊びは絵の中心ではありません。

私はそれをしたいのですが、方法がわかりません。左ノードの幅を右ノードと等しくなるように設定できると思いますが、左ノードには何もありません。

// 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可能性の 1 つは、の左側にa を追加して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);
于 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 に答える