簡體   English   中英

如何使用 scala 制作 FXML controller?

[英]How do I make a FXML controller with scala?

我正在嘗試使用創建一個 scala 程序,該程序使用從 JavaFX SceneBuilder 為 GUI 生成的 FXML 文件。 雖然https://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm是 java,但我以它為起點。 (找不到更好的了。)

如果我使用 controller class 的 java 版本,我可以運行此示例,如示例所示。 (Works with either the java version or a scala version of the class that extends Application and has the overridden start method in it.) But if I replace this java version controller class with a scala version, I get the error below:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)

Here is what Intellij converted the java version controller (which I call jController) to a scala version (which I call sController) when I used the menu item Code/Convert Java to Scala:

import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.text._

class sController {
  @FXML private val actiontarget: Text = null

  @FXML protected def handleSubmitButtonAction(event: ActionEvent): Unit = {
    actiontarget.setText("Sign in button pressed")
  }
}

當然,我還在生成的 FXML 文件中將 <GridPane fx:controller="jController" 更改為 <GridPane fx:controller="sController" 。

我還嘗試修改 controller 的 scala 版本,從 Intellij 生成的上述代碼開始,但沒有任何效果。

如果我更換

@FXML private val actiontarget: Text = null

@FXML private val actiontarget: Text = new Text()

然后它會毫無例外地運行,但不會響應按下的按鈕。

I also tried the javascript version, shown in the example, in place of the controller version, but I don't know anything about javascript and Intellij does not seem to have the "JavaScript and TypeScript bundled plugin" available anymore as referred to here: https://www.jetbrains.com/help/idea/2020.3/javascript-specific-guidelines.html#ws_JavaScript_before_you_start我嘗試了不同的 javascript 和 Z1E35BCBA2DBA1089C7BD0805 插件,但沒有幫助。

任何想法,或者這篇文章是否正確? https://bugs.openjdk.java.net/browse/JDK-8116127

我設法讓它運行如下:

import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.text._

class sController {
  @FXML private var actiontarget: Text = _

  @FXML protected def handleSubmitButtonAction(event: ActionEvent): Unit = {
    actiontarget.setText("Sign in button pressed")
  }
}

即使用var字段。

我將該字段保留為private ,但仍將其初始化為_null 這基本上如programmersdigest.de中所述。 但是,請注意,我對自己的應用程序還不是很遠。 可能該字段還必須公開,否則編譯器將被迫進行名稱修改並將事情搞砸。 有關更多詳細信息,請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM