2013-07-05 92 views
0

我是JavaFx的新手,我有兩個在FXMl中創建的gridPane,我想做同樣的事情,但在Javafx中。在Javafx中創建gridPane

我想創建一個擴展GridPane類,所以,當我打電話給我的課堂上我將有我在FXML PS相同的結果:如果您使用sceneBuiler,檢查網格線可見,所以你可以看到所有GridPane 謝謝

<GridPane layoutX="138.0" layoutY="72.0"> 
    <children> 
    <Label text="01" GridPane.columnIndex="1" GridPane.rowIndex="0" /> 

    <Label text="01" GridPane.columnIndex="2" GridPane.rowIndex="0" /> 

    <Label text="01" GridPane.columnIndex="3" GridPane.rowIndex="0" /> 

    <Label text="01" GridPane.columnIndex="4" GridPane.rowIndex="0" /> 

    <Label text="01" GridPane.columnIndex="0" GridPane.rowIndex="0" /> 

    </children> 

    <columnConstraints> 
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    </columnConstraints> 

    <rowConstraints> 

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 

    </rowConstraints> 

</GridPane> 

<GridPane layoutX="38.0" layoutY="102.0"> 

    <children> 

    <Label prefWidth="74.0" text="Label" GridPane.columnIndex="0" GridPane.rowIndex="0" /> 

    </children> 

    <columnConstraints> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="25.0" /> 

    </columnConstraints> 

    <rowConstraints> 

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 

    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 

    </rowConstraints> 

</GridPane> 
+0

爲什麼你不加載你的fxml來顯示你的gridpane? –

+0

我需要在Javafx中完成它 –

回答

0

這裏是我以前用y創建GridPane x和在父窗格稱爲格柵添加它(這個面板是由FXML創建)

public GridPane DessineGrille(int x, int y) { 
    ((Pane) Main.root.lookup("#grille")).getChildren().clear(); 
    GridPane gp = new GridPane(); 
    x += 2; 
    ColumnConstraints column = new ColumnConstraints(); 
    column.setPercentWidth((1.0f/((float) x))*100.0f); 
    for (int i = 1; i <= x; i++) { 
     gp.getColumnConstraints().add(column); 
    } 
    y += 2; 
    RowConstraints line = new RowConstraints(); 
    line.setPercentHeight((1.0f/((float) y))*100.0f); 
    for (int i = 1; i <= y; i++) { 
     gp.getRowConstraints().add(line); 
    } 
    gp.setGridLinesVisible(true); 
    gp.setHgap(10); 
    gp.setVgap(10); 
    Light.Distant light = new Light.Distant(); 
    light.setAzimuth(-135.0); 

    Lighting lighting = new Lighting(); 
    lighting.setLight(light); 
    lighting.setSurfaceScale(5.0); 
    gp.setEffect(lighting); 
    gp.setMinSize(((Pane) Main.root.lookup("#grille")).getWidth(), ((Pane) Main.root.lookup("#grille")).getHeight()); 
    ((Pane) Main.root.lookup("#grille")).getChildren().add(gp); 
    return gp; 

} 

爲了幫助您的例程,在法國'dessine'意思是畫畫和'烤le'menas grid(我不想僅僅通過翻譯來引入錯誤)。

它有幫助嗎?