如何在 JavaFX GridPane 的列之间设置填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20454021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:03:39  来源:igfitidea点击:

How to set padding between columns of a JavaFX GridPane?

javajavafx

提问by Click Upvote

I'm trying to show a 2-column grid in a javaFx program. This is how I'm setting up the grid:

我正在尝试在 javaFx 程序中显示一个 2 列网格。这就是我设置网格的方式:

    GridPane grid = new GridPane();

    ColumnConstraints column1 = new ColumnConstraints();
    column1.setPercentWidth(50);
    ColumnConstraints column2 = new ColumnConstraints();
    column2.setPercentWidth(50);
    grid.getColumnConstraints().addAll(column1, column2); 

Here's the problem. I want to show a small space between where one column ends, and the other starts. However, the columns are showing up as glued to one another.

这就是问题所在。我想在一列结束的地方和另一列的开始之间显示一个小空间。但是,这些列显示为彼此粘合。

Here's a screenshot:

这是一个屏幕截图:

screenshot

截屏

Here each column contains the 'item name' and 'process, edit, delete' buttons.

这里的每一列都包含“项目名称”和“处理、编辑、删除”按钮。

You can see how the columns are glued together. I want them to instead have some space between them.

您可以看到列是如何粘合在一起的。我希望它们之间有一些空间。

How can I solve this?

我该如何解决这个问题?

The hierarchy of my overall UI is this:

我的整个 UI 的层次结构是这样的:

Scene > ScrollPane > BorderPane > Vbox (Center) > GridPane

场景 > ScrollPane > BorderPane > Vbox (Center) > GridPane

采纳答案by assylias

For better appearance you can use a mix of:

为了获得更好的外观,您可以混合使用:

grid.setHgap(10); //horizontal gap in pixels => that's what you are asking for
grid.setVgap(10); //vertical gap in pixels
grid.setPadding(new Insets(10, 10, 10, 10)); //margins around the whole grid
                                             //(top/right/bottom/left)

回答by jewelsea

In addition to assylias's answer, you can configure the margin for any given node within the grid:

除了 assylias 的答案,您还可以为网格中的任何给定节点配置边距:

GridPane.setMargin(node, new Insets(5, 10, 5, 10));

GridPane.setMarginjavadoc:

GridPane.setMarginjavadoc:

Sets the margin for the child when contained by a gridpane. If set, the gridpane will lay it out with the margin space around it. Setting the value to null will remove the constraint.

当被网格窗格包含时,设置子项的边距。如果设置,gridpane 将用它周围的边距空间进行布局。将该值设置为 null 将删除约束。

Eventually (post Java 8), JavaFX may allow you to set a margin around any node, not just those in a GridPane.

最终(Java 8 之后),JavaFX 可能允许您在任何节点周围设置边距,而不仅仅是 GridPane 中的那些。