使用FXML时出现IllegalArgumentException

Nat*_*ich 3 tornadofx

我一直在关注使用FXML的TornadoFX指南(https://github.com/edvin/tornadofx/wiki/FXML),但是收到错误:

java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
Run Code Online (Sandbox Code Playgroud)

这是我的BoardView.kt视图:

class BoardView : View() {
    override val root: BorderPane by fxml()
    val hello: Label by fxid()

    init {
        hello.text = "Hello World"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是FXML文件(在同一个包中,ui.view)*

<BorderPane xmlns="http://javafx.com/javafx/null" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets top="20" right="20" bottom="20" left="20" />
    </padding>
    <center>
        <HBox alignment="CENTER" spacing="10">
            <Label fx:id="hello">
                <font>
                    <Font size="20"/>
                </font>
            </Label>
        </HBox>
    </center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,这是完整的堆栈跟踪:

java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
    at tornadofx.UIComponent$fxml$1.<init>(Component.kt:360)
    at tornadofx.UIComponent.fxml(Component.kt:353)
    at tornadofx.UIComponent.fxml$default(Component.kt:353)
    at ui.view.BoardView.<init>(BoardView.kt:12)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at tornadofx.FXKt.find(FX.kt:238)
    at tornadofx.App.start(App.kt:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)

我试过给fxml()提供参数,从"BoardView"到"BoardView.fxml"将fxml文件移动到src/main/resources中.我看不到任何明显的Component.kt源文件.

感谢你给与我的帮助.

Edv*_*yse 6

简短回答:将文件放在src/main/resources/ui/view/BoardView.fxml中,不要为fxml()调用提供路径参数.

如果你使用Maven,默认情况下它不会将src/main/java中的fxml文件复制到目标目录,所以即使你在同一个包中有fxml文件,它也不可用,除非你指示Maven复制资源.fxml扩展名.

我建议你把它放在src/main/resources中,但要记住它也必须在同一个包中,所以正确的路径是src/main/resources/ui/view/BoardView.fxml.

或者,如果直接将它放在src/main/resources中,则必须将此path参数添加到fxml委托:

override val root : BorderPane by fxml("/BoardView.fxml")
Run Code Online (Sandbox Code Playgroud)

注意/前缀使其看起来在类路径的根目录中.

一个很好的建议是编译项目并查看输出文件夹(Maven项目默认为目标)并检查fxml文件是否在您期望的位置.