4

私はプロジェクトに取り組んでおり、2 つのボタンをシーンの中心に配置する必要があります。作業を行うための組み込みのペイン オプションが見つからなかったため、多くのバインディングを使用する必要がありました。そのためのコードは非常に長く、仕事をするためのより簡単な方法があると確信しています。ここにデモがあります: http://screencast.com/t/pvi5WLko

だから私が知りたいのは、できれば組み込みのペインなどを使って同じことを行う簡単な方法があるかどうかです。

ウィンドウのサイズがどのように変更されても、ボタンを中央に配置したい。例:

ここに画像の説明を入力

4

1 に答える 1

1

おそらく最も簡単な方法は、VBox を使用することです。

public void start(final Stage stage) throws Exception {
    final Button button0 = new Button("Start learning");
    final Button button1 = new Button("Customize");

    final VBox box = new VBox();
    box.setFillWidth(true);

    box.getChildren().setAll(button0, button1);
    box.setAlignment(Pos.CENTER);

    stage.setScene(new Scene(box));
    stage.setWidth(200);
    stage.setHeight(100);
    stage.show();
}

これを行う別の可能な方法は、GridPane を使用することです。

public void start(final Stage stage) throws Exception {
  final Button button0 = new Button("Start learning");
  final Button button1 = new Button("Customize");

  final GridPane cPane = new GridPane();
  cPane.getChildren().addAll(button0, button1);
  GridPane.setConstraints(button0, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER);
  GridPane.setConstraints(button1, 0, 1, 1, 1, HPos.CENTER, VPos.CENTER);

  final ColumnConstraints columnn0 = new ColumnConstraints();
  columnn0.setPercentWidth(100);
  cPane.getColumnConstraints().addAll(columnn0);

  final RowConstraints row0 = new RowConstraints(1);
  row0.setPercentHeight(50);

  final RowConstraints row1 = new RowConstraints(1);
  row1.setPercentHeight(50);

  cPane.getRowConstraints().addAll(row0, row1);

  stage.setScene(new Scene(cPane));
  stage.setWidth(200);
  stage.setHeight(100);
  stage.show();
}

ここでの考え方は、グリッド内の行と列を構成して、それに応じた制約オブジェクトを使用してシーンを埋めることです。上記は、1 つの列と 2 つの行を定義します。次に、GridPane.setConstraints(...) を使用して、Grid のセル内でコンポーネントを整列できます。

ボタンをくっつけたいかどうかに応じて、上のボタンを VPos.BOTTOM に、下のボタンを VPos.TOP に揃えるように、コードを少し変更したい場合があります (両方のマージンを定義する必要があります)。もちろん)。

于 2012-11-03T10:27:53.633 に答える