简体   繁体   English

无法加载javafx场景

[英]unable to load a javafx scene

I have a problem with loading a file. 我在加载文件时遇到问题。 I'm trying to load the scene "areaView.fxml" with the following code: 我正在尝试使用以下代码加载场景“ 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();
}}

But when I launch I get an InvocationTargetException .. 但是当我启动时,我得到一个InvocationTargetException ..

For information i'm on windows and the absolute path of the file is: C:\\Users\\pierr\\Desktop\\Yves\\resources\\areaView.fxml 有关信息,我在Windows上,文件的绝对路径为: C:\\ Users \\ pierr \\ Desktop \\ Yves \\ resources \\ areaView.fxml

It's probably a stupid mistake but I've been stuck on it for quite some time.... I'm almost sure it's related to the "/" but all my attempts to fix it have failed... 这可能是一个愚蠢的错误,但是我已经坚持了很长时间。...我几乎可以确定它与“ /”有关,但是我修复它的所有尝试都失败了...

Thank you in advance 先感谢您

EDIT : 编辑:

Here the full stack tace 这里是完整的堆栈

在此处输入图片说明

EDIT n°2 编辑n°2

Very strange thing when I test this: 当我测试这个很奇怪的事情:

 System.out.println(getClass().getResource("").toString());

I get this : file:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/ 我得到这个文件:/ C:/用户/ pierr /桌面/ Yves / out / production / Yves / view /

When I would have imagined getting this: file:/C:/Users/pierr/Desktop/Yves/ 当我想到得到这个文件时file:/ C:/ Users / pierr / Desktop / Yves /

If i move my file to file:/C:/Users/pierr/Desktop/Yves/out/production/Yves/view/ it works but in fact this solution is not satisfactory 如果我将文件移动到文件:/ C:/ Users / pierr / Desktop / Yves / out / production / Yves / view /,则可以,但是实际上此解决方案并不令人满意

By default your resources folder should be under projectName/src/main/resources . 默认情况下,您的资源文件夹应位于projectName/src/main/resources

So in your case: C:\\Users\\pierr\\Desktop\\Yves\\src\\main\\resources\\areaView.fxml . 因此,在您的情况下: C:\\Users\\pierr\\Desktop\\Yves\\src\\main\\resources\\areaView.fxml

In such case .getResource("areaView.fxml") should be sufficient. 在这种情况下, .getResource("areaView.fxml")就足够了。

Just keep in mind that if you move your fxml to a different folder or you call getResource from a different folder than src/main/java you will have to change the relative path that you pass to the method. 请记住,如果将fxml移动到另一个文件夹,或者从不同于src/main/java文件夹调用getResource ,则必须更改传递给该方法的相对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM