ScalaFx和Akka演员

时间:2018-01-12 14:00:36

标签: scala akka actor scalafx akka-actor

几个星期前,我开始在Scala编程并使用Akka Actors。我试图实现一个使用Akka ActorSystem来提供多个UI的scala程序。我目前正在尝试设置一个ScalaFx应用程序,并通过内部actor将其连接到我的actor系统。因此,我编写了以下代码来设置我的actor系统和我的控制器:

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取代。"串。有人可以告诉我,我的方法是否正确,或者我做错了什么?

0 个答案:

没有答案