我的目标是创建一个可调整大小的GridView,始终为正方形,并包含相等数量的行和列,使它们的单元格也为正方形,类似于Reversi或Chess板。

这是一个小插图,网格在内容窗格上水平居中。

java - 方格的方格-LMLPHP

我尝试了许多不同的绑定变体和布局,但是我不太正确。这是我的控制器(到目前为止):

public class Controller {

    public HBox contentPane;

    public void initialize() {
        final int sideLength = 10;

        final GridPane gridPane = new GridPane();

        gridPane.setStyle("-fx-border-color: red; -fx-border-insets: 2");

        HBox.setHgrow(gridPane, Priority.ALWAYS);

        for (int i = 0; i < sideLength; i++) {
            final ColumnConstraints columnConstraints = new ColumnConstraints(Control.USE_PREF_SIZE, 10, Double.MAX_VALUE);
            columnConstraints.setHgrow(Priority.ALWAYS);
            gridPane.getColumnConstraints()
                    .add(columnConstraints);

            final RowConstraints rowConstraints = new RowConstraints(Control.USE_PREF_SIZE, 10, Double.MAX_VALUE);
            rowConstraints.setVgrow(Priority.ALWAYS);

            gridPane.getRowConstraints()
                    .add(rowConstraints);
        }

        contentPane.getChildren().add(gridPane);

        for (int i = 0; i < sideLength; i++) {
            for (int j = 0; j < sideLength; j++) {
                final GameCell child = new GameCell();
                GridPane.setRowIndex(child, i);
                GridPane.setColumnIndex(child, j);
                gridPane.getChildren().add(child);
            }
        }
    }
}


和应该包含Shapes后期的单元格,但是我现在只测试Circle来进行测试:

public class GameCell extends VBox {

    private final Circle circle;

    public GameCell() {
        circle = new Circle();

        setMinSize(0, 0);
        setPrefSize(Control.USE_COMPUTED_SIZE, Control.USE_COMPUTED_SIZE);
        getChildren().add(circle);

        final ChangeListener<Number> listener = (observable, oldValue, newValue) ->
                circle.setRadius((int) (Math.min(this.getWidth(), this.getHeight()) / 2));

        widthProperty().addListener(listener);
        heightProperty().addListener(listener);
    }
}


当前是这样的:

java - 方格的方格-LMLPHP

最佳答案

解决了很多问题,以下是我的解决方案,供以后参考:

GameCell:

public class GameCell extends Pane {
    public GameCell() {
        final Circle circle = new Circle(10);
        circle.radiusProperty().bind(Bindings.divide(widthProperty(), 4));

        circle.centerXProperty().bind(widthProperty().divide(2));
        circle.centerYProperty().bind(widthProperty().divide(2));

        getChildren().add(circle);
    }
}


GamePane:

public class GamePane extends HBox {
    public GamePane() {
        final VBox vBox = new VBox();

        vBox.alignmentProperty().set(Pos.CENTER);
        alignmentProperty().set(Pos.CENTER);

        final GridPane gridPane = new GridPane();

        final NumberBinding binding = Bindings.min(widthProperty(), heightProperty());

        gridPane.setMinSize(200, 200);
        vBox.prefWidthProperty().bind(binding);
        vBox.prefHeightProperty().bind(binding);
        vBox.setMaxSize(Control.USE_PREF_SIZE, Control.USE_PREF_SIZE);

        vBox.setFillWidth(true);
        VBox.setVgrow(gridPane, Priority.ALWAYS);

        final int sideLength = 8;
        for (int i = 0; i < sideLength; i++) {
            final ColumnConstraints columnConstraints = new ColumnConstraints(Control.USE_PREF_SIZE, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
            columnConstraints.setHgrow(Priority.SOMETIMES);
            gridPane.getColumnConstraints().add(columnConstraints);

            final RowConstraints rowConstraints = new RowConstraints(Control.USE_PREF_SIZE, Control.USE_COMPUTED_SIZE, Double.MAX_VALUE);
            rowConstraints.setVgrow(Priority.SOMETIMES);
            gridPane.getRowConstraints().add(rowConstraints);
        }

        vBox.getChildren().add(gridPane);

        getChildren().add(vBox);

        HBox.setHgrow(this, Priority.ALWAYS);

        for (int i = 0; i < sideLength; i++) {
            for (int j = 0; j < sideLength; j++) {
                final Pane child = new GameCell();

                GridPane.setRowIndex(child, i);
                GridPane.setColumnIndex(child, j);
                gridPane.getChildren().add(child);
            }
        }
    }
}

10-06 01:08