如何在 JavaFx 的 GridPane 中添加空行



我想在循环中每x行的行之间添加很少的空格。我发现向 GridPane 添加空行比对行设置特定约束更好。问题是我不知道我应该在该行中放入哪个节点来伪造空元素。我可以通过放置"说文本"节点来做到这一点。但这真的正确吗?任何人都可以提供更优雅的解决方案吗?

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

使用带有空字符串的文本节点来创建空网格窗格行是可以的。

作为替代方法,下面的示例使用 Pane 为空网格行创建一个"弹簧"节点,该节点可以将其首选高度设置为任何所需的值,以实现所需的任何间隙大小。 此外,如有必要,还可以通过 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();
  }
}

我认为解决此问题的最佳方法是添加 RowConstraint,设置Gridpane中每一行的高度。然后,您不必添加"空"行,因为无论它是否包含任何内容,每一行都将获得相同的空间。

下面是一个最小、完整且可验证的示例:

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 中的行数量,也没有简单的方法将相同的RowConstraint添加到GridPane中的每一行。这使得代码相当混乱。但是你可以通过例如创建自己的子类来跟踪大小的GridPane来解决这个问题。

在上面的例子中,我们设置了行的 pref 高度,但如果你不知道每个标签需要多少空间,你也可以使用 .setPercentHeight(double)。

GridPane gp = new GridPane();
gp.add(" ",2, 2);

最新更新