java 尝试在 JavaFX 中使用 css 样式表时出现“loadStylesheetUnPrivileged”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33764821/
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
"loadStylesheetUnPrivileged" error when trying to use css stylesheet with JavaFX
提问by Mazzone
I've read through every article/post I can find about this error, and I've tried every solution mentioned and the error is still being produced at run time. So here's my code, and below that is the error message from the console:
我已经通读了我能找到的关于此错误的所有文章/帖子,并且我已经尝试了提到的所有解决方案,但在运行时仍然会产生该错误。所以这是我的代码,下面是来自控制台的错误消息:
public class Driver extends Application {
public static void main(String[] args) {
launch(args);
} // main
@Override
public void start(Stage primaryStage) {
Parent root = null;
File css = new File("stylesheet.css");
try {
root = FXMLLoader.load(getClass().getResource("project-3.fxml"));
root.getStylesheets().clear();
root.getStylesheets().add("file:///" + css.getAbsolutePath().replace("\", "/"));
} catch (IOException e) {
System.out.println(e);
System.exit(1);
} // try
primaryStage.setTitle("Programmer's Calculator");
primaryStage.setScene(new Scene(root, 397, 376));
primaryStage.show();
} // start
} // Driver
} // 司机
I've excluded the import statements to save space - they're not the issue.
我已经排除了导入语句以节省空间 - 它们不是问题。
Here's the error produced:
这是产生的错误:
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not find stylesheet: file:////Users/UserName/Documents/Names-p3/stylesheet.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO:找不到样式表:file:////Users/UserName/Documents/Names-p3/stylesheet.css
Here's my directory:
这是我的目录:
Here's what I've tried:
这是我尝试过的:
- Created a scene variable and tried applying the css stylesheet to that
- Added the 'stylesheets' modifier or whatever it's called to the Pane element in my fxml file, like this:
<Pane stylesheets="stylesheet.css">
- Changed the directory naming to everything imaginable...
- 创建了一个场景变量并尝试将 css 样式表应用于该变量
- 向我的 fxml 文件中的 Pane 元素添加了“样式表”修饰符或任何它调用的内容,如下所示:
<Pane stylesheets="stylesheet.css">
- 将目录命名更改为可以想象的所有内容...
Literally nothing is working. What is going on?
从字面上看没有任何工作。到底是怎么回事?
回答by Collins Abitekaniza
Put the file containing your stylesheet in the src
folder and then apply it to your root.
将包含样式表的文件放在文件src
夹中,然后将其应用到您的根目录。
root = FXMLLoader.load(getClass().getResource("project-3.fxml"));
root.getStylesheets().add(getClass().getResource("your_stylesheet.css").toExternalFo??rm());
Or
或者
root.getStylesheets().add(getClass().getResource("your_stylesheet.css").toString());
回答by Vaclav Konecny
One simple solution is add at sign to FXML like this.
一种简单的解决方案是像这样将 at 符号添加到 FXML。
<Pane stylesheets="@stylesheet.css">
rather than:
而不是:
<Pane stylesheets="stylesheet.css">
More info https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html
更多信息 https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html
回答by Atspulgs
The following have worked for me:
以下对我有用:
- Resources (file has to be with the class files):
scene.getStylesheets().add(getClass().getResource("style.css").toString());
- Relative (files in working dir):
scene.getStylesheets().add("file:style.css");
- Absolute (files still in working dir):
scene.getStylesheets().add("file:/"+System.getProperty("user.dir").replace("\\", "/")+"/style.css");
- 资源(文件必须与类文件一起):
scene.getStylesheets().add(getClass().getResource("style.css").toString());
- 相对(工作目录中的文件):
scene.getStylesheets().add("file:style.css");
- 绝对(文件仍在工作目录中):
scene.getStylesheets().add("file:/"+System.getProperty("user.dir").replace("\\", "/")+"/style.css");
It would seem that it's looking for the format of: file:<path>
and for absolute paths its still looking for that root '/' even on windows. It also doesn't seem to like backslash '\' in the path.
它似乎正在寻找以下格式:file:<path>
并且对于绝对路径,它仍然在寻找那个根 '/' 即使在 Windows 上。它似乎也不喜欢路径中的反斜杠“\”。