如何在JavaFx中的GridPane中添加空行?

Kam*_*mil 3 javafx

我想在循环中每x行的行之间添加一点空格.我发现将空行添加到GridPane比在行上设置特定约束更好.问题是我不知道应该在哪一个节点放入假行空元素.我可以通过放开说文本节点来做.但这真的是对的吗?谁能提供更优雅的解决方案?

gridPane.addRow(i, new Text(""));
Run Code Online (Sandbox Code Playgroud)

jew*_*sea 7

使用带有空字符串的Text节点来创建空的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();
  }
}
Run Code Online (Sandbox Code Playgroud)