如何在JavaFx的GridPane中添加空行?
我希望在循环中每x行之间添加一点行距。我发现将空行添加到GridPane比在行上设置特定约束更好。问题是,我不知道应该在该行中放置哪个节点来伪造空元素。我可以通过放置let say文本节点来完成。但这真的正确吗?谁能提供更优雅的解决方案如何在JavaFx的GridPane中添加空行?,javafx,Javafx,我希望在循环中每x行之间添加一点行距。我发现将空行添加到GridPane比在行上设置特定约束更好。问题是,我不知道应该在该行中放置哪个节点来伪造空元素。我可以通过放置let say文本节点来完成。但这真的正确吗?谁能提供更优雅的解决方案 gridPane.addRow(i, new Text("")); 使用带有空字符串的文本节点来创建空的gridpane行是可以的 作为替代方案,下面的示例使用一个窗格为空网格行创建一个“spring”节点,该节点可以将其首选高度设置为任何所需的值,以实现所需
gridPane.addRow(i, new Text(""));
使用带有空字符串的文本节点来创建空的gridpane行是可以的 作为替代方案,下面的示例使用一个窗格为空网格行创建一个“spring”节点,该节点可以将其首选高度设置为任何所需的值,以实现所需的间隙大小。此外,如果需要,还可以通过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();
}
}
我认为解决这个问题的最好方法是添加,在
网格窗格中设置每行的高度。这样就不必添加“空”行,因为每一行都将获得相同的空间,而不管它是否包含任何内容
下面是一个例子:
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.RowConstraints;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类SSCCE扩展应用程序{
@凌驾
公众假期开始(阶段){
VBox root=新的VBox();
GridPane GridPane=新建GridPane();
添加(新标签(“第一”),0,0);
添加(新标签(“第二”),0,2;
添加(新标签(“第三”),0,3;
//为每行添加一个RowConstraint。这里的问题是
//必须知道网格窗格中有多少行要设置
//所有这些约束的行约束。
对于(int i=0;i),此链接对您很有用。
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();
}
}
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();
}
}