2009-09-28 22 views
7

私はJavafxで非常に単純なアプリケーションを作成しています。そこには、ステージ上にテキストボックスを1つのシーンとして持つ単一のボタンがあります。ここで、ボタンをクリックすると、ステージ上に別のボタンとテキストボックスがあるシーンを選択し、前のテキストボックスと共にクリックしたボタンを削除します。ボタンをクリックするとステージ上に新しいシーンがロードされます。どのように私はこれを行うことができます上の任意のヒント?JavaFXの複数のシーン

エリックのアドバイスに従う:私はこのコードを持っていて、私が望むように働いています。

var showScene1 = true; 
var showScene2 = false; 
var showScene3 = false; 

def stage = Stage 
{ 
    title: "Hello World" 

    var scene1 =Scene 
    { 
      content: 
      [ 

            Text { 
          font : Font { 
            size: 24 
          } 
          x: 10, y: 30 
          content: "HelloWorld from Scene1" 
        }, 
        Button 
         { 
          text: "Click Me to change to Scene2 " 
          onMouseClicked: function(e: MouseEvent):Void 
          { 

            showScene2 = true; 

            println("In scene 2"); 

          } 

         } 


      ] 
    } 



    var scene2 =Scene 
     { 
       content: 
       [ 

             Text { 
           font : Font { 
             size: 24 
           } 
           x: 10, y: 30 
           content: "HelloWorld from Scene2" 
         }, 
         Button 
          { 
           text: "Click Me to change to Scene3 " 
           onMouseClicked: function(e: MouseEvent):Void 
           { 
             showScene1 = false; 
             showScene2 = false; 
             showScene3 = true; 
             println("In scene 3"); 

           } 

          } 


       ] 
     } 

    var scene3 =Scene 
     { 
       content: 
       [ 

             Text { 
           font : Font { 
             size: 24 
           } 
           x: 10, y: 30 
           content: "HelloWorld from Scene3" 
         } 


       ] 
     } 


scene: bind if (showScene2) then scene2 
    else if (showScene1) then scene1 
    else scene3 

} 

答えて

1

あなたが唯一の2種類のシーンを持っていると確信している場合、あなたはちょうどそうのような舞台のシーンプロパティをバインドすることができます

var showSecondScene = false; 
var myButton = Button { 
    onMouseClicked: function(e) { showSecondScene = true; } 
} 
def stage = Stage { 
    scene: bind if (showSecondScene) then secondScene else firstScene 
} 

UPDATE:あなたは、任意の数を持っている場合、これは実際に動作しますそのような場面:代わりに、グループ・ノードを重ねる上:あなたの代わりに「偽見える」設定を選ぶ、2つの以上のシーンを持っているだろう、なぜ

scene: bind if (showScene1) then scene1 
    else if (showScene2) then scene2 
    else scene3 

あなたは考えるかもしれません。

+0

: 最初のボタンののfuctionでは、showScene2 = true;と交換してください。私が試すことができる他の方法はありますか? – iceman

+0

私は自分の答えを更新しました。複数のシーンを必要とするか、または複数のCustomNodesだけで適切な可視性を設定するかどうかを確かめてください。 –

+0

デバッグプリントステートメントをシーン宣言内に入力して、ロードまたは呼び出されているかどうかを知ることはできますか?私はシーン内のメソッドを呼び出すことはできません。 – iceman

1

すべてのif-elseステートメントを削除します。現在のシーンを含む変数に直接バインドします。

import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.text.Text; 
import javafx.scene.text.Font; 

var currentScene: Scene; 


def scene1: Scene = Scene { 
    content: Text { 
     font : Font { 
      size : 24 
     } 
     x: 10, y: 30 
     content: "Scene 1" 
     onMouseClicked: function(e):Void { 
     currentScene = scene2; 
     } 
    } 
} 

def scene2: Scene = Scene { 
    content: Text { 
     font : Font { 
      size : 24 
     } 
     x: 10, y: 30 
     content: "Scene 2" 
     onMouseClicked: function(e):Void { 
     currentScene = scene1; 
     } 
    } 
} 

Stage { 
    title: "Multi-Scene" 
    width: 250 
    height: 80 
    scene: bind currentScene 
} 

currentScene = scene1; 
0

あなたの最初のシーンと同じようにセコンシーンを作成できます。私は2つの以上のシーンを持っているとのJavaFXは、シーンのバインド後のif-elseまたはスイッチケースを許可していないので、この技術は2つのシーンのためだけで結構ですstage.setScene(scene2);

関連する問題