在 JavaFX 中的窗格之间切换 [英] Switch between panes in JavaFX

查看:28
本文介绍了在 JavaFX 中的窗格之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FXML 在 JavaFX 中制作 Java 程序.但是我在布局管理方面遇到了麻烦.我想在窗格之间切换,就像我习惯在 Swing 中使用 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.

我用谷歌搜索并没有找到任何答案.

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 代码

Here is my FXML code

    <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:

  1. 通过创建新场景和 在你的舞台上设置那个场景
  2. 通过从父布局中删除旧窗格并添加新窗格(通过操作父级的 儿童列表) 或
  3. 将所有窗格放在 StackPane 中,然后将要显示的窗格移动到 堆栈的子列表.
  1. Replace the whole scene by creating a new scene and set that scene on your Stage OR
  2. 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
  3. 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:

Angela 的解决方案是使用 StackPane 使用单独的自定义 ScreenController 类来管理 Transitions动画 堆栈中的窗格之间.

Angela's solution is to use a StackPane with a separate custom ScreenController class for managing Transitions or animations between panes in the stack.

框架

JFXFlowWebFX 还可以为您的应用提供浏览器风格的界面,允许用户使用后退和前进按钮以及历史列表在屏幕之间来回切换.

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:

还有很多其他的(我不会在这里提供一个完整的列表).

And numerous others (I won't provide a comprehensive list here).

相关

这篇关于在 JavaFX 中的窗格之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆