Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX-将图像添加到ColorPicker_Java_Css_Javafx_Color Picker - Fatal编程技术网

JavaFX-将图像添加到ColorPicker

JavaFX-将图像添加到ColorPicker,java,css,javafx,color-picker,Java,Css,Javafx,Color Picker,我正在使用netBeans和SceneBuilder创建一个带有一些文本编辑器选项的小窗口。我在屏幕中包含的一个按钮是一个颜色选择器,我根据以下风格对其进行了修改: <ColorPicker fx:id="clrFill" layoutX="119.0" layoutY="18.0" prefHeight="30.0" prefWidth="30.0" style="-fx-color-label-visible: false; -fx-color-rect-height: 3; -fx-

我正在使用netBeans和SceneBuilder创建一个带有一些文本编辑器选项的小窗口。我在屏幕中包含的一个按钮是一个颜色选择器,我根据以下风格对其进行了修改:

<ColorPicker fx:id="clrFill" layoutX="119.0" layoutY="18.0" prefHeight="30.0" prefWidth="30.0" style="-fx-color-label-visible: false; -fx-color-rect-height: 3; -fx-color-rect-width: 17;" styleClass="button" stylesheets="/css/colorPickerStyle.css" />
我的目标是在显示当前颜色的修改后的颜色选择器“矩形”上方显示图像。最初,我尝试将colorPicker视为Button类,因为我已将样式更改为“Button”,但无法使用setGraphic方法放置图像。正如您从css文件中看到的,我还尝试实现了一个背景图像,但没有成功


是我的屏幕当前外观的副本。colorPicker位于ComboBox下拉列表旁边。

如果您试图更改css文件中的colorPicker样式类,那么这将不起作用,您不能将代码放在那里

实际上,要使用图像,您不需要更改其样式类,只需要使用
url(image.png)
(图像与FXML放在同一文件夹中),或者使用到FXML文件的相对路径,如
url(../image.png)
,或者在绝对路径的情况下使用
@

这应该起作用:

.color-picker {
    -fx-background-image: url(fill.png);
    -fx-background-size: 20 20;
    -fx-background-position: top center;
    -fx-background-repeat: no-repeat;   
}
.color-picker .color-picker-label .picker-color {
    -fx-alignment : bottom-center;
} 
您可以检查图像的
格式

.color-picker {
    -fx-background-image: url(fill.png);
    -fx-background-size: 20 20;
    -fx-background-position: top center;
    -fx-background-repeat: no-repeat;   
}
.color-picker .color-picker-label .picker-color {
    -fx-alignment : bottom-center;
}