2012-03-22 115 views
3

我有一個簡單的問題。網格能否自動調整其中的對象以適應網格?Gridpane可以自動調整其對象的大小嗎?嘗試將max_width和max_height設置爲網格並使其調整內容大小。 JavaFX

我想在網格上設置max_height和max_width以及最小寬度和最小高度,並在我的窗口中居中該網格。

在我的網格中,我想在所有位置添加Spacer Square。每個墊片都有邊框。所以它看起來像一個實際的網格。

如果我的網格是4x4,我想那裏有16個大方格。

如果我的網格是16x18,我想那裏有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); 
    } 
} 
+1

我希望能使用網格來放置網格列下的png圖像。使用網格可以讓我輕鬆地保存png圖像。有沒有辦法做到這一點與網格? – CREW 2012-03-23 00:40:48

回答

4

如果你想要一個可調整大小的矩形字段,你不需要一個網格。只要把它們放在窗格上,並綁定到窗格大小:

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(); 
} 
+0

謝謝謝爾蓋 我曾希望使用一個網格還可以將png圖像放置在網格列下方。使用網格可以讓我輕鬆地保存png圖像。有沒有辦法做到這一點與網格? – CREW 2012-03-23 00:40:28

+0

請你詳細說明「跌倒」? – 2012-03-23 07:24:06

相關問題