java Javafx Gridpane 单元格背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25070896/
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
Javafx Gridpane Cell Background Colors
提问by lumisphere902
So I have a javafx program which procedurally creates text nodes and inserts them into a GridPane. Each of these text nodes is given a CSS class. I used a stylesheet, to try to make the cells of each class a different color. However, I can only use the -fx-fill
property to change the text color, which is good, but I cannot find anything online as to how to change the background of each cell through CSS or Java. I have also tried the fx-background-color
property in the CSS, but that doesn't work. Any help would be appreciated; thanks.
所以我有一个 javafx 程序,它按程序创建文本节点并将它们插入到 GridPane 中。这些文本节点中的每一个都有一个 CSS 类。我使用了一个样式表,试图使每个类的单元格具有不同的颜色。但是,我只能使用该-fx-fill
属性来更改文本颜色,这很好,但是我在网上找不到关于如何通过 CSS 或 Java 更改每个单元格的背景的任何信息。我也尝试过fx-background-color
CSS 中的属性,但这不起作用。任何帮助,将不胜感激; 谢谢。
回答by James_D
You need to use a Region
, or some subclass of Region
, in order to set the background color.
您需要使用Region
或 的某个子类Region
来设置背景颜色。
Either:
任何一个:
Use a Label
instead of a Text
. You can set the text color with -fx-text-fill
and the background color with -fx-background-color
.
使用 aLabel
代替 a Text
。您可以使用 设置文本颜色-fx-text-fill
和背景颜色-fx-background-color
。
Or:
或者:
Create a StackPane
for each Text
; add the Text
to the StackPane
and the StackPane
to the GridPane
. You can set the text color with -fx-fill
on the Text
and the background color with -fx-background-color
on the StackPane
. You will probably need to call
StackPane
为每个创建一个Text
;添加Text
到StackPane
和StackPane
到GridPane
。您可以设置文字颜色-fx-fill
上Text
和背景色-fx-background-color
上StackPane
。你可能需要打电话
GridPane.setFillHeight(myStackPane, true);
GridPane.setFillWidth(myStackPane, true);
on each StackPane
to get the StackPane
to fill the GridPane
cell.
在每个StackPane
上StackPane
以填充GridPane
单元格。
回答by p.JANGID
You can use stackpane as well change background color of stackpane. Add the node of single cell in stackpane and after you need to add stackpane in grid
您可以使用 stackpane 以及更改 stackpane 的背景颜色。在stackpane中添加单个单元格的节点,然后需要在grid中添加stackpane
grid.getChildren.add(pane,0,1);