我在 javafx 中有一些代码,它有一个像 Canvas 一样工作的 Pane 。我需要能够从颜色选择器填充 Pane 的背景颜色。
目前我有一个颜色变量,它获取从颜色选择器中选择的颜色,我尝试将其设置到我的 Pane (名为 Canvas ),如下所示:
Color newColour = backgroundColourPicker.getValue();
canvas.setStyle("-fx-background-color: " + newColour + ";");
但是我得到这个输出:
June 11, 2022 7:47:57 PM javafx.css.CssParser term
WARNING: CSS Error parsing '*{-fx-background-color: 0x00ffffff;}: Unexpected token '0x' at [1,24]
如何将颜色值交换为字符串以便能够删除前导 0x 并使其正常工作,或者如何让我的 Pane 接受颜色值作为颜色?
最佳答案
我发现如果将来有人需要这个代码,它对我有用:
Color newColour = backgroundColourPicker.getValue();
Double red = newColour.getRed()*100;
int rInt = red.intValue();
Double green = newColour.getGreen()*100;
int gInt = green.intValue();
Double blue = newColour.getBlue()*100;
int bInt = blue.intValue();
String hex = String.format("#%02X%02X%02X", rInt, gInt, bInt);
canvas.setStyle("-fx-background-color: " + hex + ";");
关于java - 如何从颜色选择器设置 Pane 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72583321/