2013-01-15 32 views
0

使用主要參數我想用這個代碼從我的程序顯示一般的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: _*) 
    } 
    } 

如何傳遞場景參數的孔帕尼翁對象?

例如,我想創建一個程序,它從這個階腳本執行打開一個新的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) 
} 

回答

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: _*) 
    } 
} 

編輯:現在這個工作,根據您的意見。

+0

當然,如果你不需要對創建的實例的引用,你可以將'scene'成員定義爲所有'traits'中的一個方法 –

+0

嗨,工作,對象和類需要相同的名稱,所以' JFXWindowDisplay'需要是一個類,並且我們還需要'classOf [JFXWindowsDisplay]'到objet啓動器'JFXWindowDisplay' :(很難找到答案 – reyman64