栅格窗格可以自动调整其对象的大小以适应吗? 尝试将max_width和max_height设置为网格并让其调整内容大小。 JavaFX的

我有一个简单的问题。 网格是否可以自动调整其中的对象以适应网格?

我想在网格上设置max_height和max_width以及最小宽度和最小高度,并在窗口中居中该网格。

在我的网格中,我想在所有位置添加Spacer Square。 每个垫片都有边框。 所以它看起来像一个实际的网格。

如果我的网格是4×4,我希望有16个大正方形。

如果我的网格是16×18,我希望有288个方格。

两个选项都应占用一定数量的区域,288方形选项的正方形比16选项小很多,以适应我的网格尺寸。

我检查了gridpane文档,但如果有一个选项,我很困惑。 我不知道填充,边距,setmaxwidth,setmaxheight之间的区别(试过这个,没有改变一件事)。

double dimension_x=100; //max_width of actual square/spacer/gridspace double dimension_y=100; //max_height of actual square/spacer/gridspace int grid_x=100; //number of rows int grid_y=100; //number of columns Rectangle[][] rectangles = new Rectangle[grid_x][grid_y]; GridPane grid = new GridPane(); double grid_max_x=800; double grid_max_y=600; grid.setHgap(1); grid.setVgap(1); grid.setPadding(new Insets(16)); //not sure what this does. Attempt Fail grid.setEffect(addEffect(Color.web("#202C2F"), .61, 12)); grid.setMaxHeight(grid_max_y); //does nothing that it APPEARS to me for (int x=0;x<grid_x;x++) { for(int y=0;y<grid_y;y++) { Rectangle temp = new Rectangle(dimension_x,dimension_y); grid.add(temp,x,y); } } 

如果您想要一个可resize的矩形字段,则不需要网格。 只需将它们放在窗格上并绑定到窗格大小:

 public void start(Stage stage) { Pane root = new Pane(); final int count = 7; //number of rectangles NumberBinding minSide = Bindings .min(root.heightProperty(), root.widthProperty()) .divide(count); for (int x = 0; x < count; x++) { for (int y = 0; y < count; y++) { Rectangle rectangle = new Rectangle(0, 0, Color.LIGHTGRAY); rectangle.xProperty().bind(minSide.multiply(x)); rectangle.yProperty().bind(minSide.multiply(y)); rectangle.heightProperty().bind(minSide.subtract(2)); rectangle.widthProperty().bind(rectangle.heightProperty()); root.getChildren().add(rectangle); } } stage.setScene(new Scene(root, 500, 500)); stage.show(); }