2012-08-13 88 views
3

我想在循環中每行x行之間添加一點空間。我發現在GridPane上添加空行比在行上設置特定約束更好。問題是我不知道應該將哪個節點放入該行來僞造空元素。我可以通過放置說文本節點。但這真的是對的嗎?誰能提供更優雅的解決方案?如何在JavaFx的GridPane中添加空行?

gridPane.addRow(i, new Text("")); 

回答

5

使用文本節點用於創建空gridpane行的一個空字符串是好的。

作爲替代,樣品下方使用窗格創建這可能會對它的首選高度設置爲任何所需的值來實現你想要的任何間隙大小的空網格行了「春天」節點。此外,如果需要,還可以通過css對彈簧節點進行樣式設置。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

// GridPane with a blank row 
// http://stackoverflow.com/questions/11934045/how-to-add-empty-row-in-gridpane-in-javafx 
public class GridPaneWithEmptyRowSample extends Application { 
    public static void main(String[] args) { launch(args); } 
    @Override public void start(final Stage stage) throws Exception { 
    // create nodes for the grid. 
    final Label label1 = new Label("Label 1"); 
    final Label label2 = new Label("Label 2"); 
    final Label label3 = new Label("Label 3"); 
    final Pane spring = new Pane(); 
    spring.minHeightProperty().bind(label1.heightProperty()); 

    // layout the scene. 
    final GridPane layout = new GridPane(); 
    layout.add(label1, 0, 0); 
    layout.add(spring, 0, 1); 
    layout.add(label2, 0, 2); 
    layout.add(label3, 0, 3); 
    layout.setPrefHeight(100); 
    stage.setScene(new Scene(layout)); 
    stage.show(); 
    } 
} 
-6
GridPane gp = new GridPane(); 
gp.add(" ",2, 2); 
+0

此示例代碼不使用JavaFX 2.x的 – jewelsea 2012-08-13 17:11:59

+0

編譯此鏈接是對您有用。 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html – 2012-08-14 06:29:13

3

我認爲解決這個最好的辦法就是通過增加RowConstraints,設定各行的高度在Gridpane。那麼你不必添加「空行」,因爲每行都將獲得相同的空間,無論它是否包含任何內容。

這裏有一個minimal, complete and verifiable例如:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.RowConstraints; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class SSCCE extends Application { 

    @Override 
    public void start(Stage stage) { 

     VBox root = new VBox(); 

     GridPane gridPane = new GridPane(); 

     gridPane.add(new Label("First"), 0, 0); 
     gridPane.add(new Label("Second"), 0, 2); 
     gridPane.add(new Label("Third"), 0, 3); 

     // Add one RowConstraint for each row. The problem here is that you 
     // have to know how many rows you have in you GridPane to set 
     // RowConstraints for all of them. 
     for (int i = 0; i <= 3; i++) { 
      RowConstraints con = new RowConstraints(); 
      // Here we set the pref height of the row, but you could also use .setPercentHeight(double) if you don't know much space you will need for each label. 
      con.setPrefHeight(20); 
      gridPane.getRowConstraints().add(con); 
     } 

     root.getChildren().add(gridPane); 

     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.show(); 

    } 

    public static void main(String[] args) { 
     launch(); 
    } 
} 

這種方法的問題是,有 - 據我所知 - 以獲得在GridPane量行沒有簡單的方法,而且也沒有簡單的方法來添加與GridPane中的每一行相同RowConstraint。這使得代碼相當混亂。但你可以通過例如爲GridPane創建自己的子類,以跟蹤大小。

在上面的例子中,我們設置行的PREF高度,但你也可以使用.setPercentHeight(雙),如果你不知道多少空間,你需要爲每個標籤。