使用 FXML 时出现 IllegalArgumentException
IllegalArgumentException when using FXML
我一直在关注使用 FXML 的 TornadoFX 指南 (https://github.com/edvin/tornadofx/wiki/FXML),但遇到错误:
java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
这是我的 BoardView.kt 观点:
class BoardView : View() {
override val root: BorderPane by fxml()
val hello: Label by fxid()
init {
hello.text = "Hello World"
}
}
这是 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>
如果有帮助,这里是完整的堆栈跟踪:
java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
at tornadofx.UIComponent$fxml.<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$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
我尝试为 fxml() 提供参数,从 "BoardView" 到 "BoardView.fxml" 到将 fxml 文件移动到 src/main/resources。查看 Component.kt 源文件时,我看不到任何明显的内容。
感谢您能给我的任何帮助。
首先简短回答:将文件放在 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 中,您必须将此路径参数添加到 fxml 委托中:
override val root : BorderPane by fxml("/BoardView.fxml")
注意 / 前缀,使其在类路径的根目录中查找。
一个好的提示是编译项目并查看输出文件夹(Maven 项目默认为 target)并检查 fxml 文件是否位于您期望的位置.
我一直在关注使用 FXML 的 TornadoFX 指南 (https://github.com/edvin/tornadofx/wiki/FXML),但遇到错误:
java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
这是我的 BoardView.kt 观点:
class BoardView : View() {
override val root: BorderPane by fxml()
val hello: Label by fxid()
init {
hello.text = "Hello World"
}
}
这是 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>
如果有帮助,这里是完整的堆栈跟踪:
java.lang.IllegalArgumentException: FXML not found for class ui.view.BoardView
at tornadofx.UIComponent$fxml.<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$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(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$null9(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
我尝试为 fxml() 提供参数,从 "BoardView" 到 "BoardView.fxml" 到将 fxml 文件移动到 src/main/resources。查看 Component.kt 源文件时,我看不到任何明显的内容。
感谢您能给我的任何帮助。
首先简短回答:将文件放在 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 中,您必须将此路径参数添加到 fxml 委托中:
override val root : BorderPane by fxml("/BoardView.fxml")
注意 / 前缀,使其在类路径的根目录中查找。
一个好的提示是编译项目并查看输出文件夹(Maven 项目默认为 target)并检查 fxml 文件是否位于您期望的位置.