chr*_*sen 21 java swing netbeans javafx scenebuilder
我正在尝试使用FXML在JavaFX中创建Java程序.但是我在布局管理方面遇到了麻烦.我想在Panes之间切换,就像我习惯使用CardLayout一样,但我似乎无法得到它.
我用Google搜索,没有找到任何答案.
JavaFX中是否有任何CardLayout等效项?如果是的话,你能给我一个例子吗?这对我的晚会很有帮助!
这是我的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>
Run Code Online (Sandbox Code Playgroud)
jew*_*sea 34
非动画过渡
如果您不需要在窗格之间进行动画过渡,则可以:
StackPane然后将要显示的窗格移动到堆栈子列表的顶部.动画过渡
如果您希望在窗格之间进行动画转换,请参阅Angela Caicedo关于在JavaFX中管理多个屏幕的两部分系列:
Angela的解决方案是使用StackPane和单独的自定义ScreenController类来管理堆栈中窗格之间的转换或动画.
构架
像JFXFlow和WebFX这样的框架也可以为您的应用程序提供浏览器样式界面,允许用户使用后退和前进按钮以及历史列表在屏幕之间来回切换.
2017年更新
我认为上面两个引用框架的开发现在已经不存在了.其他正在开发的框架是:
还有很多其他人(我不会在这里提供全面的清单).
有关
下面是我的做法:(在本例中,我创建了两个 FXML 文档及其对应的控制器。它们分别称为 FXMLLogin.fxml 和 Home.fxml)。
所以,要从 FXMLLogin 转到 Home,
在此示例中,我在FXMLLoginController 中创建了一个方法,该方法响应按下该表单上的“登录”按钮:
@FXML
private void login(javafx.event.ActionEvent event) throws IOException
{
if(pwf1.getText().equals("alphabetathetagamma"))
{
Parent blah = FXMLLoader.load(getClass().getResource("Home.fxml"));
Scene scene = new Scene(blah);
Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
appStage.setScene(scene);
appStage.show();
}
else
{
label1.setText("Password is incorrect. Please Try Again");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,@FXML 非常重要。
如果我正确理解了您的问题,那么这应该可以解决问题。
在窗格之间切换一点也不明显,并且在我发现的网络上的任何教程中都没有清楚地概述。在我第一次弄清楚之前,我不得不自己广泛地谷歌搜索。幸运的是,一旦你掌握了它,它实际上很简单。
我希望我没有误解你的问题?让我知道这是否是您需要的:)