[英]ScalaFX i18n with fxml not working
First the working code: 首先是工作代码:
val root: BorderPane = new BorderPane(jfxf.FXMLLoader.load(getClass.getResource("/GUI/main.fxml")))
stage = new PrimaryStage()
{
title = "FXML Test"
scene = new Scene(root)
}
No problem here. 没问题 Now I wanted to add i18n support like so:
现在,我想像这样添加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)
}
Now the constructor scene = new Scene(root)
cannot be resolved. 现在,无法解析构造函数
scene = new Scene(root)
。
I tried to solve this by 我试图通过解决这个问题
1) initializing a new BorderPane, like: 1)初始化一个新的BorderPane,例如:
val root = new BorderPane(loader.load[jfxs.Parent])
But the constructor of BorderPane cannot be resolved so I tried 但是BorderPane的构造函数无法解析,所以我尝试了
2) casting it to a BorderPane, like: 2)将其转换为BorderPane,例如:
val root = new BorderPane(loader.load[jfxs.Parent].asInstanceOf[BorderPane])
which is okay in the IDE but throws a compiler-error: 在IDE中可以,但是会引发编译器错误:
Caused by: java.lang.ClassCastException: javafx.scene.layout.BorderPane cannot be cast to scalafx.scene.layout.BorderPane
由以下原因引起:java.lang.ClassCastException:javafx.scene.layout.BorderPane无法转换为scalafx.scene.layout.BorderPane
How can I resolve this? 我该如何解决?
"Now the constructor scene = new Scene(root)
cannot be resolved.": that would be because the scalafx.scene.Scene
constructor expects a parameter of type scalafx.scene.Parent
and not javafx.scene.Parent
. “现在无法解析构造函数
scene = new Scene(root)
。”:那是因为scalafx.scene.Scene
构造函数需要一个scalafx.scene.Parent
类型的参数,而不是javafx.scene.Parent
类型的参数。
Just add import scalafx.Includes._
to your imports to use ScalaFX's implicit conversions. 只需将
import scalafx.Includes._
添加到您的导入中即可使用ScalaFX的隐式转换。 You can then do: 然后,您可以执行以下操作:
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.