欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaFX實現界面跳轉

 更新時間:2022年06月16日 16:36:58   作者:西子~  
這篇文章主要為大家詳細介紹了JavaFX實現界面跳轉,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

界面跳轉,很常見的一個功能,在桌面程序中,可以多窗口跳轉,也可以在一個窗口中跳轉。不同方式對應不同場景。下面簡單介紹一下,JavaFX中單窗口界面跳轉方式。

BorderPane 跳轉

利用BorderPane的setCenter重新設置中心節(jié)點進行界面跳轉。

好處是其他區(qū)域的節(jié)點不會更新,只會更新center中的節(jié)點,并且可以控制是每個頁面是否可以重新加載,方便。

scene節(jié)點如下,在BorderPane的top中設置按鈕事件,更新center。

fxml

<BorderPane prefHeight="200.0" prefWidth="200.0" fx:id="container">
? ? ? ? ?<top>
? ? ? ? ? ? <HBox alignment="CENTER" spacing="20.0" BorderPane.alignment="CENTER">
? ? ? ? ? ? ? ?<children>
? ? ? ? ? ? ? ? ? <Button mnemonicParsing="false" text="首頁" onAction="#toHome" />
? ? ? ? ? ? ? ? ? <Button mnemonicParsing="false" text="文件" onAction="#toFile"/>
? ? ? ? ? ? ? ? ? <Button mnemonicParsing="false" text="設置" onAction="#toSetting"/>
? ? ? ? ? ? ? ?</children>
? ? ? ? ? ? ? ?<padding>
? ? ? ? ? ? ? ? ? <Insets bottom="10.0" top="10.0" />
? ? ? ? ? ? ? ?</padding>
? ? ? ? ? ? </HBox>
? ? ? ? ?</top>
? ? ? ? ?<center>
? </center>
</BorderPane>

controller

public class JumpController {

? ? public BorderPane container;

? ? public void initialize() {
? ? ? ? URL resource = getClass().getResource("/fxml/jump/home.fxml");
? ? ? ? try {
? ? ? ? ? ? setCenter(resource);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? private void setCenter(URL url) throws IOException {
? ? ? ? FXMLLoader loader = new FXMLLoader(url);
? ? ? ? loader.load();
? ? ? ? Parent root = loader.getRoot();
? ? ? ? container.setCenter(root);
? ? }

? ? public void toHome(ActionEvent event) {
? ? ? ? URL resource = getClass().getResource("/fxml/jump/home.fxml");
? ? ? ? try {
? ? ? ? ? ? setCenter(resource);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? public void toFile(ActionEvent event) {
? ? ? ? URL resource = getClass().getResource("/fxml/jump/file.fxml");
? ? ? ? try {
? ? ? ? ? ? setCenter(resource);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? public void toSetting(ActionEvent event) {
? ? ? ? URL resource = getClass().getResource("/fxml/jump/setting.fxml");
? ? ? ? try {
? ? ? ? ? ? setCenter(resource);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}

StackPane跳轉

StackPane也是JavaFX中的一個面板容器,特點是里面的元素是堆疊在一起的,每次只顯示最上層元素。利用這個特點,可以把多個界面加載之后作為StackPane的字節(jié)的,然后調整StackPane的頂層元素即可。

這種方法比較適合每個頁面跳轉時不需要重新加載的情況,效率比較高,只是改變字節(jié)點的順序。

fxml

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="529.0" prefWidth="785.0"
? ? ? xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="xyz.yuelai.controller.Jump1Controller">
? ?<HBox alignment="CENTER" spacing="20.0">
? ? ? <children>
? ? ? ? ?<Button mnemonicParsing="false" onAction="#toHome" text="首頁" />
? ? ? ? ?<Button mnemonicParsing="false" onAction="#toFile" text="文件" />
? ? ? ? ?<Button mnemonicParsing="false" onAction="#toSetting" text="設置" />
? ? ? </children>
? ? ? <padding>
? ? ? ? ?<Insets bottom="10.0" top="10.0" />
? ? ? </padding>
? ?</HBox>
? ?<StackPane prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS" fx:id="container" />

</VBox>

controller

public class Jump1Controller {

? ? public StackPane container;
? ? private Parent home;
? ? private Parent file;
? ? private Parent setting;

? ? public void initialize() {
? ? ? ? try {
? ? ? ? ? ? URL homeUrl = getClass().getResource("/fxml/jump/home.fxml");
? ? ? ? ? ? home = getParent(homeUrl);
? ? ? ? ? ? URL fileUrl = getClass().getResource("/fxml/jump/file.fxml");
? ? ? ? ? ? file = getParent(fileUrl);
? ? ? ? ? ? URL settingUrl = getClass().getResource("/fxml/jump/setting.fxml");
? ? ? ? ? ? setting = getParent(settingUrl);

? ? ? ? ? ? container.getChildren().addAll(setting, file, home);
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? private Parent getParent(URL url) throws IOException {
? ? ? ? FXMLLoader loader = new FXMLLoader(url);
? ? ? ? return loader.load();
? ? }

? ? public void toHome(ActionEvent event) {
? ? ? ? home.toFront();
? ? }

? ? public void toFile(ActionEvent event) {
? ? ? ? file.toFront();
? ? }

? ? public void toSetting(ActionEvent event) {
? ? ? ? setting.toFront();
? ? }
}

三個界面的fxml如下:

首頁

<AnchorPane prefHeight="460.0" prefWidth="781.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
? ?<children>
? ? ? <Label alignment="CENTER" layoutX="297.0" layoutY="131.0" prefHeight="110.0" prefWidth="129.0" style="-fx-background-color: #a00;" text="首頁" textFill="WHITE" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="200.0" AnchorPane.topAnchor="100.0">
? ? ? ? ?<font>
? ? ? ? ? ? <Font name="System Bold" size="20.0" />
? ? ? ? ?</font>
? ? ? </Label>
? ?</children>
</AnchorPane>

文件

<AnchorPane prefHeight="460.0" prefWidth="781.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
? ? <children>
? ? ? ? <Label alignment="CENTER" layoutX="297.0" layoutY="131.0" prefHeight="110.0" prefWidth="129.0" style="-fx-background-color: #0a0;" text="文件" textFill="WHITE" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="200.0" AnchorPane.topAnchor="100.0">
? ? ? ? ? ? <font>
? ? ? ? ? ? ? ? <Font name="System Bold" size="20.0" />
? ? ? ? ? ? </font>
? ? ? ? </Label>
? ? </children>
</AnchorPane>

設置

<AnchorPane prefHeight="460.0" prefWidth="781.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
? ? <children>
? ? ? ? <Label alignment="CENTER" layoutX="297.0" layoutY="131.0" prefHeight="110.0" prefWidth="129.0" style="-fx-background-color: #00a;" text="設置" textFill="WHITE" AnchorPane.leftAnchor="200.0" AnchorPane.rightAnchor="200.0" AnchorPane.topAnchor="100.0">
? ? ? ? ? ? <font>
? ? ? ? ? ? ? ? <Font name="System Bold" size="20.0" />
? ? ? ? ? ? </font>
? ? ? ? </Label>
? ? </children>
</AnchorPane>

其他跳轉方式,比如重新設置scene,這就相當于重新加載當前窗口,如非必要還是不推薦。上面兩種方式都是操作的容器里面的節(jié)點。實現了視覺上的界面跳轉。所以不局限于BorderPane和StackPane,只是這兩個容器用起來比較方便。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中JVM的雙親委派、內存溢出、垃圾回收和調優(yōu)詳解

    Java中JVM的雙親委派、內存溢出、垃圾回收和調優(yōu)詳解

    這篇文章主要介紹了Java中JVM的雙親委派、內存溢出、垃圾回收和調優(yōu)詳解,類加載器是Java虛擬機(JVM)的一個重要組成部分,它的主要作用是將類的字節(jié)碼加載到內存中,并生成對應的Class對象,需要的朋友可以參考下
    2023-07-07
  • Java用freemarker導出word實用示例

    Java用freemarker導出word實用示例

    本篇文章主要介紹了Java用freemarker導出word實用示例,使用freemarker的模板來實現功能,有需要的可以了解一下。
    2016-11-11
  • SpringDataJpa如何使用union多表分頁條件查詢

    SpringDataJpa如何使用union多表分頁條件查詢

    這篇文章主要介紹了SpringDataJpa如何使用union多表分頁條件查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 教你怎么用java一鍵自動生成數據庫文檔

    教你怎么用java一鍵自動生成數據庫文檔

    最近小編也在找這樣的插件,就是不想寫文檔了,浪費時間和心情啊,果然我找到一款比較好用,操作簡單不復雜.screw 是一個簡潔好用的數據庫表結構文檔的生成工具,支持 MySQL、Oracle、PostgreSQL 等主流的關系數據庫.需要的朋友可以參考下
    2021-05-05
  • 淺談基于Token的WEB后臺認證機制

    淺談基于Token的WEB后臺認證機制

    這篇文章主要介紹了淺談基于Token的WEB后臺認證機制,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • MyBatis在mapper中傳遞參數的四種方式

    MyBatis在mapper中傳遞參數的四種方式

    MyBatis是一個持久層框架,它提供了一種將數據庫操作與Java對象之間的映射關系進行配置的方式,在MyBatis中,Mapper是用于定義數據庫操作的接口,而參數傳遞則是通過Mapper接口的方法來實現的,本文給大家介紹了MyBatis在mapper中傳遞參數的四種方式,需要的朋友可以參考下
    2024-03-03
  • spring-data-jpa中findOne與getOne的區(qū)別說明

    spring-data-jpa中findOne與getOne的區(qū)別說明

    這篇文章主要介紹了spring-data-jpa中findOne與getOne的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot?如何將項目打包成?jar?包

    SpringBoot?如何將項目打包成?jar?包

    這篇文章主要介紹了SpringBoot如何將項目打包成jar包,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法

    Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法

    這篇文章主要介紹了Spring Boot 與 kotlin 使用Thymeleaf模板引擎渲染web視圖的方法,本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 淺談利用Session防止表單重復提交

    淺談利用Session防止表單重復提交

    這篇文章主要介紹了淺談利用Session防止表單重復提交,簡單介紹表單重復提交的情況,分析,以及解決方法代碼示例,具有一定借鑒價值,需要的朋友可以了解下。
    2017-12-12

最新評論