ScalaFXML 应用程序找不到控制器

ScalaFXML Application Cannot Find Controller

我有一个 ScalaFX 应用程序,我正在使用 FXML 作为我的视图。我在将视图连接到控制器时遇到了麻烦。除了 Github 上的示例,我在网上找不到太多信息,即使我试图模仿他们是如何做到的,它也不起作用。以下是我在 FXML 中的连接方式。

imports...

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="520.0" minWidth="880.0" prefHeight="520.0"
            prefWidth="880.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="scala/scalafxml/ToolbarController">
<center....

这是我的控制器class...

@sfxml
class ToolbarController extends JFXApp {
  //open new form
   def newForm = {
    val resource = getClass.getResource("/scala/scalafxml/form.fxml")

堆栈跟踪表明它无法找到 fx:controller 引用的控制器 class。在这里。

Exception in Application start method
Workaround until RT-13281 is implemented: keep toolkit alive
[error] (run-main-0) java.lang.RuntimeException: Exception in Application start method
java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication2(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda/777613804.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/Users/patrickslagle/scala/MyApps/PattyCakesWorksheet/target/scala-2.10/classes/scalafxml/calendar.fxml:9

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:918)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
    at FXMLMain$delayedInit$body.apply(FXMLMain.scala:16)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scalafx.application.JFXApp$$anonfun$init.apply(JFXApp.scala:297)
    at scalafx.application.JFXApp$$anonfun$init.apply(JFXApp.scala:297)
    at scala.collection.immutable.List.foreach(List.scala:318)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
    at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
    at scalafx.application.JFXApp$class.init(JFXApp.scala:297)
    at FXMLMain$.init(FXMLMain.scala:10)
    at scalafx.application.AppHelper.start(AppHelper.scala:33)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication19(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda/206252937.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1146564181.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/354390534.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater1(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1198063642.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.ClassNotFoundException: scala/scalafxml/ToolbarController
    at java.lang.ClassLoader.findClass(ClassLoader.java:530)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
    at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 11 s, completed May 24, 2016 3:32:56 PM

FXML 文件位于 src/main/resources/scalafxml/calendar.fxml,控制器位于 src/main/scala/scalafxml/ToolbarController.scala.

我可能有明显的错误,因为我对 Scala 还很陌生。非常感谢任何指点。

看看这个独立的例子: https://github.com/vigoo/scalafxml-unit-converter-example

您应该有一个单独的控制器 class 和一个 JFXApp,并使用 ScalaFXML 中的FXMLView 构造函数来创建视图。