ScalaFX @sfxml 注释问题

ScalaFX @sfxml Annotation Problems

我正在编写一个 Scalafx 应用程序,使用 FXML 为带有 scalafxml 库的视图。在控制器 类 上使用 @sfxml 注释时,我在编译 类 时遇到了很多麻烦。使用它们时,我的构造函数不断出现 NoSuchMethodExceptions,就好像我没有默认构造函数一样,但事实并非如此。我一直在研究这个,所以我只能得出结论,问题一定出在图书馆本身。我已尽我所知关注有关它的任何信息。

控制器用于为用户创建表单。由于如果我尝试两次设置根节点的场景将抛出 "IllegalArgumentException",因此我在构造函数中使用根节点的 id 以避免异常。

非常感谢任何帮助。谢谢

我的代码:

@sfxml
class ToolbarController(private val rootLayout: BorderPane) {

  val parent = calcParent

  import javafx.application.Platform
  def newForm = {
    new JFXPanel()
    Platform.runLater(new Runnable() {
      override def run() {

        val dialogStage = new Stage {
            scene = new Scene(parent) {
              stylesheets = List(getClass.getResource("/scala/css/form.css").toExternalForm)
            }
        }
        dialogStage.show()
        }
    })
  }


  def calcParent: Parent = {
    val resource = getClass.getResource("scala/form.fxml")
    if(rootLayout.scene == null)
      FXMLView(resource, NoDependencyResolver)
    else rootLayout.getParent
  }
}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane id="rootLayout"maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FormController">
   <center>...

还有我的堆栈跟踪:

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/1268584418.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/Users/patrickslagle/scala/MyApps/PattyCakes/target/scala-2.11/classes/scala/calendar.fxml:7

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
    at javafx.fxml.FXMLLoader.access0(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:928)
    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 Calendar$.delayedEndpoint$Calendar(Calendar.scala:15)
    at Calendar$delayedInit$body.apply(Calendar.scala:9)
    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:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
    at scalafx.application.JFXApp$class.init(JFXApp.scala:297)
    at Calendar$.init(Calendar.scala:9)
    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/2116169040.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1458556428.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/778909025.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/1397409682.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.InstantiationException: controller.ToolbarController
    at java.lang.Class.newInstance(Class.java:427)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923)
    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 Calendar$.delayedEndpoint$Calendar(Calendar.scala:15)
    at Calendar$delayedInit$body.apply(Calendar.scala:9)
    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:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
    at scalafx.application.JFXApp$class.init(JFXApp.scala:297)
    at Calendar$.init(Calendar.scala:9)
    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/2116169040.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1458556428.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/778909025.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/1397409682.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NoSuchMethodException: controller.ToolbarController.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923)
    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 Calendar$.delayedEndpoint$Calendar(Calendar.scala:15)
    at Calendar$delayedInit$body.apply(Calendar.scala:9)
    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:383)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
    at scalafx.application.JFXApp$class.init(JFXApp.scala:297)
    at Calendar$.init(Calendar.scala:9)
    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/2116169040.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait2(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1458556428.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null0(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/778909025.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/1397409682.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
[trace] Stack trace suppressed: run last pattycakes/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 pattycakes/compile:run for the full output.
[error] (pattycakes/compile:run) Nonzero exit code: 1
[error] Total time: 46 s, completed May 26, 2016 4:18:42 PM

您不应该直接实例化带有 @fxml 注释的 class。此 class 由 SFXML 编译器宏修改。它没有显示在您发布的代码中,但您必须在某处实例化 ToolbarController 才能调用 newForm。发帖Minimal, Complete, and Verifiable example很重要

尝试直接实例化 ToolbarControllerNoSuchMethodExceptions 的最可能原因,因为 ToolbarController 构造函数被宏修改了。您需要将方法 newFormcalcParent 移到 ToolbarController 之外。仅使用 FXMLView(resource, ...) 实例化 ToolbarController。您还需要在顶部窗格的 FXML 文件中添加对 ToolbarController 的引用:

<BorderPane ... fx:controller="mypackage.ToolbarController"> ...

因此 FXMLView 可以仅使用对 FXML 文件的引用来实例化 ToolbarController。例如,实例化可能如下所示:

import java.io.IOException
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafxml.core.{NoDependencyResolver, FXMLView}

object MyFXMLDemo extends JFXApp {

  val resource = getClass.getResource("/scala/css/form.css")
  if (resource == null) {
    throw new IOException("Cannot load resource: /scala/css/form.css")
  }

  val root = FXMLView(resource, NoDependencyResolver)

  stage = new PrimaryStage() {
    scene = new Scene(root)
  }

}

请仔细查看 https://github.com/vigoo/scalafxml-unit-converter-example 中的示例。