我在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
}
答案 0 :(得分:1)
如果你确定你只有2个不同的场景,你可以像这样绑定舞台的场景属性:
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
您可能会考虑为什么您有两个以上的场景,而是选择在重叠的组节点上设置'visible:false'。
答案 1 :(得分: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;
答案 2 :(得分:0)
你可以像第一个那样制作一个secon场景。在功能
您使用showScene2 = true;
stage.setScene(scene2);
的第一个按钮