簡體   English   中英

具有fxml的ScalaFX i18n無法正常工作

[英]ScalaFX i18n with fxml not working

首先是工作代碼:

val root: BorderPane = new BorderPane(jfxf.FXMLLoader.load(getClass.getResource("/GUI/main.fxml")))

stage = new PrimaryStage()
{
  title = "FXML Test"
  scene = new Scene(root)
}

沒問題 現在,我想像這樣添加i18n支持:

val bundle: ResourceBundle = new PropertyResourceBundle(getClass.getResource("/i18n/en.properties").openStream)
val loader: FXMLLoader = new FXMLLoader(getClass.getResource("/GUI/main.fxml"), bundle)
val root = loader.load[jfxs.Parent]

stage = new PrimaryStage()
{
  title = "FXML Test"
  scene = new Scene(root)
}

現在,無法解析構造函數scene = new Scene(root)

我試圖通過解決這個問題

1)初始化一個新的BorderPane,例如:

val root = new BorderPane(loader.load[jfxs.Parent])

但是BorderPane的構造函數無法解析,所以我嘗試了

2)將其轉換為BorderPane,例如:

val root = new BorderPane(loader.load[jfxs.Parent].asInstanceOf[BorderPane])

在IDE中可以,但是會引發編譯器錯誤:

由以下原因引起:java.lang.ClassCastException:javafx.scene.layout.BorderPane無法轉換為scalafx.scene.layout.BorderPane

我該如何解決?

“現在無法解析構造函數scene = new Scene(root) 。”:那是因為scalafx.scene.Scene構造函數需要一個scalafx.scene.Parent類型的參數,而不是javafx.scene.Parent類型的參數。

只需將import scalafx.Includes._添加到您的導入中即可使用ScalaFX的隱式轉換。 然后,您可以執行以下操作:

import java.util.PropertyResourceBundle
import javafx.fxml.FXMLLoader
import javafx.{scene => jfxs}

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene

object MyApp extends JFXApp{
  val bundle = new PropertyResourceBundle(getClass.getResource("/i18n/en.properties").openStream)
  val fxml = getClass.getResource("/GUI/main.fxml")
  val root: jfxs.Parent = FXMLLoader.load(fxml, bundle)

  stage = new PrimaryStage() {
    title = "FXML Test"
    scene = new Scene(root) // root is implicitly converted to scalafx.scene.Parent
  }
}

暫無
暫無

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

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