【发布时间】:2012-08-13 12:09:59
【问题描述】:
我想在循环中的每 x 行之间添加一点空间。我发现向 GridPane 添加空行比在行上设置特定约束更好。问题是我不知道应该在该行中放入哪个节点来伪造空元素。我可以通过让我们说文本节点来做到这一点。但这真的正确吗?谁能提供更优雅的解决方案?
gridPane.addRow(i, new Text(""));
【问题讨论】:
标签: javafx
我想在循环中的每 x 行之间添加一点空间。我发现向 GridPane 添加空行比在行上设置特定约束更好。问题是我不知道应该在该行中放入哪个节点来伪造空元素。我可以通过让我们说文本节点来做到这一点。但这真的正确吗?谁能提供更优雅的解决方案?
gridPane.addRow(i, new Text(""));
【问题讨论】:
标签: javafx
使用带有空字符串的文本节点来创建空的网格窗格行是可以的。
作为替代方案,下面的示例使用窗格为空网格行创建一个“弹簧”节点,可以将其首选高度设置为任何所需的值,以实现您想要的任何间隙大小。此外,如果需要,也可以通过 css 设置 spring 节点的样式。
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();
}
}
【讨论】:
我认为解决这个问题的最好方法是添加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 中的数量行,也没有简单的方法可以将相同的 RowConstraint 添加到 GridPane 中的每一行。这使得代码相当混乱。但是你可以通过例如解决这个问题为跟踪大小的 GridPane 创建自己的子类。
在上面的示例中,我们设置了行的首选项高度,但如果您不知道每个标签需要多少空间,您也可以使用 .setPercentHeight(double)。
【讨论】:
GridPane gp = new GridPane();
gp.add(" ",2, 2);
【讨论】: