object Game {
// Main class of the game in which the actor system gets started
def main(args: Array[String]): Unit = {
val actorSystem = ActorSystem.create("gameActorSystem")
val controller = actorSystem.actorOf(Props[Controller], "controller")
var gui = actorSystem.actorOf(Props(new GuiApp.GuiActor(controller)), "guiActor")
println("Start Game")
gui ! StartGame
gui ! "Hello ScalaFx App."
}
}
class Controller extends Actor {
private val observers = scala.collection.mutable.SortedSet.empty[ActorRef]
override def receive: Receive = {
case StartGame => startGame()
case RegisterObserver => observers += sender(); sender ! PrintMessage("Subscription from: [" + sender().toString() + "]")
}
... controller code
}
然后我实现了一个简单的ScalaFx应用程序:
object GuiApp extends JFXApp {
class GuiActor(controller: ActorRef) extends Actor {
controller ! RegisterObserver
override def receive: Receive = {
case StartGame => start()
case s:String => Platform.runLater{
label.text = "Message: " + s
}
}
}
def start(): Unit = {
val args = Array.empty[String]
main(args)
}
val label = new Label("Loading ...")
stage = new PrimaryStage {
title = "ScalaFX Game"
scene = new Scene {
root = new BorderPane {
padding = Insets(25)
center = label
}
}
}
}
所以我基本上要做的就是启动我的scala程序,然后注册akka系统并注册控制器和gui actor。注册gui actor之后,我想启动GUI,然后告诉GUI设置" Hello ScalaFx App。"字符串到GUI上。
到目前为止,ScalaFx GUI启动了,但GUI的初始内容是"正在加载......"并没有被" Hello ScalaFx App取代。"串。有人可以告诉我,我的方法是否正确,或者我做错了什么?