7

我正在用 Javafx 编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击该按钮时,我可以使用另一个按钮和一个文本框加载另一个场景在舞台上并删除我单击的按钮以及上一个文本框。所以点击一个按钮应该在舞台上加载一个新场景。关于我如何做到这一点的任何提示?

遵循 Eric 的建议:我有这个代码,它的工作方式是我想要的。

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

}
4

3 回答 3

1

如果您确定只有 2 个不同的场景,您可以像这样绑定 Stage 的场景属性:

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

更新:如果你有很多这样的场景,这实际上是有效的:

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

您可能会考虑为什么会有超过 2 个场景,而不是选择在重叠的组节点上设置“可见:假”。

于 2009-09-28T17:50:31.583 回答
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;   
于 2010-01-12T06:08:26.440 回答
0

您可以像制作第一个场景一样制作第二个场景。在第一个按钮的功能中,您替换showScene2 = true;为:stage.setScene(scene2);

于 2016-01-11T21:21:00.597 回答