使用Scala中的主要参数将对象Scene传递到JavaFX窗口

时间:2013-01-15 16:56:47

标签: scala constructor initialization javafx

我想使用此代码从我的程序中显示通用的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)
}

1 个答案:

答案 0 :(得分: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: _*)
  }
}

编辑:现在根据您的评论工作。