我在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
}
Run Code Online (Sandbox Code Playgroud)
如果您确定只有 2 个不同的场景,则可以像这样绑定舞台的场景属性:
var showSecondScene = false;
var myButton = Button {
onMouseClicked: function(e) { showSecondScene = true; }
}
def stage = Stage {
scene: bind if (showSecondScene) then secondScene else firstScene
}
Run Code Online (Sandbox Code Playgroud)
更新:如果你有任意数量的场景,这实际上是有效的:
scene: bind if (showScene1) then scene1
else if (showScene2) then scene2
else scene3
Run Code Online (Sandbox Code Playgroud)
您可能会考虑为什么会有超过 2 个场景,而是选择在重叠的组节点上设置“visible: false”。
| 归档时间: |
|
| 查看次数: |
9880 次 |
| 最近记录: |