0

我想使用此代码从我的程序中显示通用 Javafx 场景

class JFXWindowDisplay(myScene:Scene) extends Application {

    def start( stage: Stage) {

      stage.setTitle("My JavaFX Application")
      stage.setScene(myScene)
      stage.show()
    }
  }

//OBJECT NEEDED TO RUN JAVAFX : http://stackoverflow.com/questions/12124657/getting-started-on-scala-javafx-desktop-application-development
  object JFXWindowDisplay extends App {

    override def main(args: Array[String]) {
      Application.launch(classOf[JFXWindowDisplay], args: _*)
    }
  }

如何将 Scene 参数传递给 compagnon 对象?

例如,我想创建一个程序,从 scala 脚本执行中打开一个新的 javafx 窗口:

class myProgram() extends App  {

  val root = new StackPane
  root.getChildren.add(new Label("Hello world!"))

  val myScene = new Scene(root, 300, 300))
  // then run ControlTest object with this scene
  ControlTest(myScene)
}
4

1 回答 1

0

您可以准备两个或更多traits每个与他的特定场景,然后将您需要的一个混合到应用程序类中

trait SceneProvider {
  def scene: Scene
}

trait Scene1 extends SceneProvider {
  val root = new StackPane
  root.getChildren.add(new Label("Hello world!"))
  def scene = new Scene(root, 300, 300)
}

trait Scene2 extends SceneProvider {
  val root = new StackPane
  root.getChildren.add(new Label("Hello world 2!"))
  def scene = new Scene(root, 300, 300)
}

trait JFXWindowDisplay {
  self: SceneProvider =>

  def start(stage: Stage) {
    stage.setTitle("My JavaFX Application")
    stage.setScene(scene)
    stage.show()
  }
}


class JFXWindowDisplay1 extends Application with JFXWindowDisplay with Scene1
class JFXWindowDisplay2 extends Application with JFXWindowDisplay with Scene2

object JFXMain extends App {
  override def main(args: Array[String]) {
    //here you chose an application class
    Application.launch(classOf[JFXWindowDisplay1], args: _*)
  }
}

编辑:现在这工作,根据您的意见。

于 2013-01-15T22:15:23.363 回答