在JavaFX中切换窗格 [英] Switch between panes in JavaFX
问题描述
我正在尝试使用FXML在JavaFX中创建Java程序。但是我在布局管理方面遇到了麻烦。我想在Panes之间切换,就像我习惯使用CardLayout一样,但我似乎无法得到它。
I'm trying to make a Java program in JavaFX using FXML. However i'm having trouble with the layout management. I want to switch between Panes, just as i'm used to with CardLayout in swing, but i can't seem to get it.
我用Google搜索并且没有找到任何答案。
I googled around and didn't find any answers.
JavaFX中是否存在任何CardLayout等效项?如果是的话,你能举一个例子吗?这对我的晚会很有帮助!
Is there any CardLayout equivalent in JavaFX? and if so, can you provide me of an example? That would help my evening a lot!
这是我的FXML代码
<AnchorPane id="anchorPane" prefHeight="324.0" prefWidth="530.0" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication2.SampleController">
<children>
<Pane fx:id="mainScreen" layoutX="6.0" prefHeight="324.0" prefWidth="518.0">
<children>
<Button layoutX="254.0" layoutY="37.0" mnemonicParsing="false" text="Button" />
</children>
</Pane>
<Pane fx:id="loginScreen" prefHeight="324.0" prefWidth="530.0">
<children>
<TextField id="password" fx:id="username" layoutX="142.0" layoutY="106.0" prefWidth="200.0" />
<TextField fx:id="password" layoutX="142.0" layoutY="140.0" prefWidth="200.0" />
<Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
<Button fx:id="button" layoutX="213.0" layoutY="196.0" onAction="#handleButtonAction" onKeyPressed="#handleButtonAction" text="Login" />
</children>
</Pane>
</children>
</AnchorPane>
推荐答案
非动画过渡
如果您不需要在窗格之间设置动画过渡,则可以:
If you don't need animated transitions between your panes, then you can:
- 通过创建新场景替换整个场景并在舞台上设置该场景或
- 通过从父布局中删除旧窗格,替换父布局中的特定窗格添加新窗格(通过操纵父窗口子列表)或
- 将所有窗格放在
StackPane
中并移动要显示的窗格到堆栈的顶部柒d列表。
- Replace the whole scene by creating a new scene and set that scene on your Stage OR
- Replace just a specific pane in a parent layout, by removing the old pane from it's parent and adding your new pane (by manipulating the parent's children list) OR
- Place all your Panes in a
StackPane
and move the pane you want to display to the top of the stack's child list.
动画过渡
如果您想在窗格之间进行动画转换,请参阅Angela Caicedo关于在JavaFX中管理多个屏幕的两部分系列:
If you would like animated transtions between your panes, then see Angela Caicedo's two part series on managing multiple screens in JavaFX:
- Part I
- Part II
Angela的解决方案是使用 StackPane ,带有一个单独的自定义ScreenController类,用于管理过渡或动画在堆栈中的窗格之间。
Angela's solution is to use a StackPane with a separate custom ScreenController class for managing Transitions or animations between panes in the stack.
框架
JFXFlow 和 WebFX 还可以为您的应用程序提供浏览器样式界面,允许用户使用后退和前进按钮在屏幕之间来回切换。历史列表。
Frameworks like JFXFlow and WebFX can also provide a browser style interface for your app, allowing users to switch back and forth between screens using back and forward buttons and a history list.
2017年更新
我认为两个引用框架的开发以上现已不复存在。正在开发的其他框架包括:
I think development on both referenced frameworks above is now defunct. Other frameworks which are under development are:
- TornadoFX
- JRebirth
- afterburner.fx
以及其他许多人(我不会在这里提供全面的清单)。
And numerous others (I won't provide a comprehensive list here).
相关
- Loading new fxml in the same scene
这篇关于在JavaFX中切换窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!