我正在尝试为应用程序中的所有按钮设置背景图像,当我通过 FXML 文件设置它时它工作正常但是当我使用 CSS 样式表设置它时它不起作用,尽管其他一些属性工作正常来自样式表,如字体属性。
这是我的样式表代码:
@font-face {
font-family: 'AdvertisingExtraBold';
src: url('/fonts/AdvertisingExtraBold.ttf');
}
@font-face {
font-family: 'Droid Arabic Kufi';
src: url('/fonts/DroidKufi-Regular.ttf');
}
@font-face {
font-family: 'Naskh';
src: url('/fonts/droid-naskh.ttf');
}
.label {
-fx-font-family: 'Kufi';
}
.button .text {
-fx-font-family: 'AdvertisingExtraBold'; // This is working fine
-fx-background-image: url('shutdown.png'); // This is not
-fx-background-repeat: no-repeat; // This is not
-fx-background-size: 40; // This is not
}
但是当我像下面这样在 FXML 文件中应用它时,它工作正常:
<Button prefHeight="40.0" prefWidth="40" style="-fx-background-image: url('shutdown.png');-fx-background-repeat: no-repeat;-fx-background-size: 40;" />
同样的代码,为什么在样式表中不起作用?
有什么区别以及为什么字体属性有效而背景图像无效?
我正在像那样成功加载样式表:
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
最佳答案
Button
具有背景属性;它包含的 Text
节点没有。内联样式之所以有效,是因为您将规则应用于按钮。所以
.button .text{
-fx-font-family: 'AdvertisingExtraBold';
}
.button {
-fx-background-image: url('shutdown.png');
-fx-background-repeat: no-repeat;
-fx-background-size: 40;
}
会起作用。
关于java - 背景属性不适用于样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46360163/