IntelliJ IDEA - getClass().getResource(“…”) return null

前端 未结 9 713
挽巷
挽巷 2020-12-15 16:21

I\'m using IntelliJ IDEA 13.1.5, I used to work with Eclipse. I\'m working on JavaFX application, I try to load FXML file within my MainApp class using getClass().getResourc

9条回答
  •  [愿得一人]
    2020-12-15 16:46

    first, you need to set src and resource folders for IntelliJ.

    see the icon near the java(blue) and resources(yellow 4 lines) folder.

    right-click on java folder and select sources root

    and right-click on java folder and select resource root

    after that create the same package in the java folder and resource folder.

    for example: packages -> org.example

    ├─ src
       ├─ main
           ├─ java
           |  └─ com
           |      └─ example
           |           └─ A.class
           |
           └─ resources  
              └─ com
                  └─ example
                        └─ fxml
                             └─ AFXML.fxml
    

    in A.class you can use this.

    Java

     FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/AFXML.fxml"));
     Parent root = loader.load();
    

    Kotlin

     val loader = FXMLLoader(javaClass.getResource("fxml/AFXML.fxml"))
     val root = loader.load()
    

    or

     val loader = FXMLLoader(A::class.java.getResource("fxml/AFXML.fxml"))
     val root = loader.load()
    

提交回复
热议问题