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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:16:44  来源:igfitidea点击:

Javafx Gridpane Cell Background Colors

javacsstextjavafx

提问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-fillproperty 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-colorproperty in the CSS, but that doesn't work. Any help would be appreciated; thanks.

所以我有一个 javafx 程序,它按程序创建文本节点并将它们插入到 GridPane 中。这些文本节点中的每一个都有一个 CSS 类。我使用了一个样式表,试图使每个类的单元格具有不同的颜色。但是,我只能使用该-fx-fill属性来更改文本颜色,这很好,但是我在网上找不到关于如何通过 CSS 或 Java 更改每个单元格的背景的任何信息。我也尝试过fx-background-colorCSS 中的属性,但这不起作用。任何帮助,将不胜感激; 谢谢。

回答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 Labelinstead of a Text. You can set the text color with -fx-text-filland the background color with -fx-background-color.

使用 aLabel代替 a Text。您可以使用 设置文本颜色-fx-text-fill和背景颜色-fx-background-color

Or:

或者:

Create a StackPanefor each Text; add the Textto the StackPaneand the StackPaneto the GridPane. You can set the text color with -fx-fillon the Textand the background color with -fx-background-coloron the StackPane. You will probably need to call

StackPane为每个创建一个Text;添加TextStackPaneStackPaneGridPane。您可以设置文字颜色-fx-fillText和背景色-fx-background-colorStackPane。你可能需要打电话

GridPane.setFillHeight(myStackPane, true);
GridPane.setFillWidth(myStackPane, true);

on each StackPaneto get the StackPaneto fill the GridPanecell.

在每个StackPaneStackPane以填充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);