2013-02-07 36 views
14

我有一個簡單的GridPane顯示幾個標籤和一個按鈕,但我似乎無法得到它適合父母StackPane。我將如何前行並使其充滿它所裝的任何集裝箱?JavaFX - 獲取GridPane以適合父母

GridPane g = new GridPane(); 
    g.add(new Label("Categories"),0,0); 
    g.add(new Label("Content"),1,0);   
    Button newCat = new Button("New Category"); 
    newCat.setOnAction(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent e) { 
      System.out.println("Clicked"); 
     } 
    });  
    g.add(newCat,0,1); 
    g.setGridLinesVisible(true); 
    StackPane root = new StackPane(); 
    root.getChildren().add(g);   
    Scene scene = new Scene(root, 300, 250); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

UI真的不是我的專長,任何幫助,將不勝感激。

回答

0

然後將GridPane放入StackPane中,您應該將您的GridPane添加到HBox或VBox中。調整Gridpane更方便。順便說一句,你仍然有一些方法,如g.setPrefSize(arg0,arg1);相應地設置您的高度和寬度。

+0

我知道,但通過設置它不」首選大小t將它縮放到父母身上,它只是設置了FX將嘗試使用的大小,但我會嘗試將其設置爲HBox,儘管我很難相信這可以解決我的問題,因爲它不會重新調整其他項目的大小像按鈕等...但我會嘗試^^ –

+0

很抱歉這麼說,但是HBox或VBox對GridPanes大小有任何影響,就像使其填充父項一樣。 –

+0

@JohnMikaelGundersen嘗試像這樣並相應地調整您的網格約束。 [鏈接] http://pastebin.com/rA0Ch8ET –

0

雖然這並不明顯,但我認爲您的GridPane已經和其父容器一樣大。 如果您在「Scene scene = new Scene(root,300,250);」之前添加以下2行代碼,您應該能夠看到發生了什麼。

root.setStyle("-fx-background-color: yellow;"); 
g.setStyle("-fx-border-color: blue;"); 
+0

它並沒有擴展到父母,據我所見 –

+0

GridPane不知道如何重新分配額外的寬度和高度。你需要定義它。你想要第一行佔用所有額外的垂直空間,第二列佔用所有額外的水平空間嗎?你有沒有考慮過使用TableView? – ytw

10

你應該可以看到這個link,尤其是約百分比來調整的一部分。如果你只有兩列,我發現代碼工作:

GridPane grid = new GridPane(); 
/* your code */ 
ColumnConstraints column1 = new ColumnConstraints(); 
column1.setPercentWidth(50); 
grid.getColumnConstraints().add(column1); 
+1

這絕對有效。我開車瘋了,因爲我的天窗不適合父母,但現在它確實如此。還有一個提示:即使超過兩個庫侖也可以工作,只需調整相應的值即可! –

+0

謝謝,我在回答中添加了註釋。 – Videl

8

如果任何人都可以尋找一個答案,這是怎麼工作對我來說:

@Override 
public void start(Stage stage) throws Exception { 
    stage.setTitle("Window title"); 
    GridPane grid = new GridPane(); 

    gridSetup(grid); // Building my grid elements here (2 columns) 

    // Setting columns size in percent 
    ColumnConstraints column = new ColumnConstraints(); 
    column.setPercentWidth(30); 
    grid.getColumnConstraints().add(column); 

    column = new ColumnConstraints(); 
    column.setPercentWidth(70); 
    grid.getColumnConstraints().add(column); 

    grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Default width and height 
    grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE); 

    Scene scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT); 
    stage.setScene(scene); 
    stage.show(); 
} 

有了這個設置網格佔用窗口的所有空間,並自動調整窗口大小。

3

我們假設你的佈局有2列3行,您可以定義列的尺寸屬性和行一個接一個:

<GridPane 
    xmlns:fx="http://javafx.com/fxml/1"  
    id="gridPane" 
    prefHeight="768.0" 
    prefWidth="1024.0">  

    <gridLinesVisible>true</gridLinesVisible> 
    <columnConstraints> 
     <ColumnConstraints percentWidth="20" /> 
     <ColumnConstraints percentWidth="80" />   
    </columnConstraints> 

    <rowConstraints> 
     <RowConstraints minHeight="50"/> 
     <RowConstraints percentHeight="80"/> 
     <RowConstraints minHeight="50"/> 
    </rowConstraints> 

</GridPane>