【发布时间】:2021-05-16 16:50:15
【问题描述】:
JDK 11、JavaFX 15。
尽管How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application? 的回答很好,这仍然让我感到困扰。它不解决加载相关资源的问题。
我有一个加载引用 CSS 文件的 FXML 文件的简单示例。
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/11.0.1">
<stylesheets>
<URL value="@/styles/root.css" />
</stylesheets>
</AnchorPane>
这是 pkg/App.java 类:
package pkg;
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(App.class.getResource("root.fxml"));
scene = new Scene(loader.load());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
这是root.css 文件:
.mainFxmlClass {
}
这是 jar 布局(消除 maven 样板、模块信息、清单)。
pkg/App.class
pkg/root.fxml
pkg/styles/root.css
styles/root.css
我得到的相关异常是:
Caused by: javafx.fxml.LoadException: Invalid resource: /styles/root.css not found on the classpath
根据Introduction to FXML,在位置分辨率下,它说:
作为字符串,XML 属性不能原生地表示类型化的位置 URL 等信息。但是,通常需要指定 标记中的此类位置;例如,图像的来源 资源。位置解析运算符(由“@”表示 属性值的前缀)用于指定一个属性 value 应被视为相对于当前文件的位置 而不是一个简单的字符串。
异常指定“/styles/root.css”,这是一个绝对路径。从 JAR 布局中可以看出,我有一个 /styles/root.css。但是文档说“相对于当前文件”。
假设“当前文件”是/pkg/root.fxml,那么/pkg/styles/root.css(应该)相对于/pkg/root.fxml(或者是/pkg/App.class?),但它也找不到这个。
如果我注释掉 stylesheets 元素,文件加载正常。
那么,我应该将root.css 文件放在哪里?
【问题讨论】:
-
由于错误消息说无法找到
/styles/root.css,我的猜测是前导斜杠会导致问题。如果你改用"@styles/root.css"会怎样? -
尝试
@../styles/root.css,对于同一个文件夹,使用 Slaw 的答案。 -
您是否验证了 jar 文件的内容(例如,从命令行使用
jar -tf)?还是您只是在源代码布局中展示您认为存在的内容? -
@James_D 这是实际的 jar 文件(减去我认为不相关的内容)。