2017-07-07 10 views
0

私の目標は、サイズ変更可能な、常に正方形で、同じ数の行と列を含むGridViewを作成して、ReversiまたはChessボードに似たセルを正方形にします。正方形セルの四角形グリッド

ここには小さな図があります。グリッドはコンテンツペインの中央に水平に配置されています。

enter image description here

私は、異なる結合変異体およびレイアウトを多数試みたが、私はかなりそれが権利を取得することはできません。

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

や形状のlatesを含めることになっている細胞、が、私は今ちょうどそれをテストするためには、Sましたが、Circle:ここに私のコントローラは(今のところ)だ

これは、それが現在どのように見えるか:

enter image description here

答えて

0

は工夫がたくさんでそれを解決し、ここに私の解決策は、今後の参考のためです:

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); 
      } 
     } 
    } 
} 
関連する問題