无法加载 javafx 场景
unable to load a javafx scene
我在加载文件时遇到问题。我正在尝试使用以下代码加载场景 "areaView.fxml":
public class View extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/resources/areaView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}}
但是当我启动时,我得到一个 InvocationTargetException..
有关信息,我在 windows 上,文件的绝对路径是:C:\Users\pierr\Desktop\Yves\resources\areaView.fxml
这可能是一个愚蠢的错误,但我已经坚持了很长一段时间....我几乎可以肯定它与“/”有关,但我所有修复它的尝试都失败了...
提前致谢
编辑:
这里是完整的堆栈教程
编辑 n°2
当我测试这个时很奇怪:
System.out.println(getClass().getResource("").toString());
我明白了:文件:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/
当我想象得到这个:file:/C:/Users/pierr/Desktop/Yves/
如果我将我的文件移动到 file:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/ 它可以工作,但实际上这个解决方案并不令人满意
默认情况下,您的资源文件夹应位于 projectName/src/main/resources
。
所以在你的情况下:C:\Users\pierr\Desktop\Yves\src\main\resources\areaView.fxml
.
在这种情况下 .getResource("areaView.fxml")
应该足够了。
请记住,如果您将 fxml 移动到不同的文件夹或从不同于 src/main/java
的文件夹调用 getResource
,您将必须更改传递给方法。
我在加载文件时遇到问题。我正在尝试使用以下代码加载场景 "areaView.fxml":
public class View extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/resources/areaView.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}}
但是当我启动时,我得到一个 InvocationTargetException..
有关信息,我在 windows 上,文件的绝对路径是:C:\Users\pierr\Desktop\Yves\resources\areaView.fxml
这可能是一个愚蠢的错误,但我已经坚持了很长一段时间....我几乎可以肯定它与“/”有关,但我所有修复它的尝试都失败了...
提前致谢
编辑:
这里是完整的堆栈教程
编辑 n°2
当我测试这个时很奇怪:
System.out.println(getClass().getResource("").toString());
我明白了:文件:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/
当我想象得到这个:file:/C:/Users/pierr/Desktop/Yves/
如果我将我的文件移动到 file:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/ 它可以工作,但实际上这个解决方案并不令人满意
默认情况下,您的资源文件夹应位于 projectName/src/main/resources
。
所以在你的情况下:C:\Users\pierr\Desktop\Yves\src\main\resources\areaView.fxml
.
在这种情况下 .getResource("areaView.fxml")
应该足够了。
请记住,如果您将 fxml 移动到不同的文件夹或从不同于 src/main/java
的文件夹调用 getResource
,您将必须更改传递给方法。