我的语法从FXML文件调用样式表(css)有什么问题? [英] What is wrong with my syntax calling a stylesheet (css) from an FXML file?
问题描述
我在NetBeans 8中使用Java 8 / JavaFX和FXML。
I am working in NetBeans 8, with Java 8 / JavaFX and FXML.
在我的FXML文档的第一个控制器(AnchorPane)中,我有这个,有效:
In the first controller (an AnchorPane) in my FXML document, I have this, that works:
stylesheets="file:///C:/Users/me/Documents/NetBeansProjects/MyApp/src/myApp/myStyle.css"
然而,当我尝试用任何类型的相对路径替换它时,我在运行时收到以下错误:
However, when I attempt to replace that with any type of relative path, I get the following error at runtime:
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "myStyle.css" not found.
我试过
stylesheets="myStyle.css"
stylesheets="file://myStyle.css"
stylesheets="./myStyle.css"
stylesheets="css/myStyle.css"
(我把css文件的副本放在一个子目录中)
(where I put a copy of the css file in a subdirectory from where it was)
...可能还有50种其他排列。每次我得到同样的错误。
...and maybe 50 other permutations. Every time I get the same error.
我关心的原因是我遇到了第二个问题。在第一个版本中,它可以在我的机器上运行...当我将我的jar文件带到另一台机器时,运行应用程序时不会应用css ...就像资源文件没有被复制一样。
The reason I care is that I am having a second problem. In the first version, that works on my machine... when I take my jar file to another machine, the css is not applied when the application is run... It's like the resource file isn't copied.
帮助?
谢谢!
-Adeena
推荐答案
让我考虑一下您的项目结构如下
Let me consider your project structure as follows
MyApp
|
|_ src
|
|_controllers (Controllers)
|_view (FXML)
|_style (css)
|_myStyle.css
您可以使用
<stylesheets>
<URL value="@../style/myStyle.css" />
</stylesheets>
在你的控制器中,你可以按如下方式添加它
In your controller, you can add it as follows
layout.getStylesheets().add(getClass().
getResource("/style/myStyle.css")).toExternalForm();
这篇关于我的语法从FXML文件调用样式表(css)有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!