JavaFX 两个窗口切换

一、效果:下面两个界面,点击“转到...”就可以相互切换

 

二、项目目录:

三、代码:

MainApp:

package cn.wyj;

import javafx.application.Application;
import javafx.stage.Stage;

public class MainApp  extends Application {

    public void start(Stage primaryStage) throws Exception {
        StageController stageController = new StageController();
        stageController.setStage(primaryStage);
        stageController.initScene1();
        stageController.initScene2();
        stageController.show_scene1();
    }
}

StageController:
package cn.wyj;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.IOException;


public class StageController {
    static Parent root1 =null;
    static Parent root2 =null;
    static Scene scene1=null;
    static Scene scene2=null;

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    private static Stage stage=null;

    public boolean initScene1() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/fxml/f1.fxml"));
        root1 = loader.load();
        return true;
    }

    public boolean initScene2() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/fxml/f2.fxml"));
        root2 = loader.load();
        return true;
    }

    public boolean show_scene1() throws IOException {
        if(stage==null||root1==null) {
            return  false;
        }
        if(scene1==null)
        {
            scene1 = new Scene(root1);
        }
        stage.setScene(scene1);
        stage.show();
        return true;
    }

    public boolean show_scene2() throws IOException {
        if(stage==null||root2==null) {
            return  false;
        }
        if(scene2==null)
        {
            scene2 = new Scene(root2);
        }
        stage.setScene(scene2);
        stage.show();
        return true;
    }

    @FXML
    void onView1Button1_click(MouseEvent event) throws IOException {
        show_scene2();
    }

    @FXML
    void view2Button1_onAction(ActionEvent event) throws IOException {
        show_scene1();
    }

}

 四、fxml文件:

f1.xml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.wyj.StageController">
   <children>
      <Button fx:id="view1Button1" layoutX="235.0" layoutY="122.0" mnemonicParsing="false" onMouseClicked="#onView1Button1_click" prefHeight="23.0" prefWidth="107.0" text="转到场景2" textFill="#0b52b4" />
      <Label layoutX="265.0" layoutY="68.0" prefHeight="24.0" prefWidth="84.0" text="场景1">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

f2.xml:

 

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cn.wyj.StageController">
   <children>
      <Button fx:id="view2Button1" layoutX="243.0" layoutY="149.0" mnemonicParsing="false" onAction="#view2Button1_onAction" text="转到场景1" />
      <Label layoutX="234.0" layoutY="77.0" text="这里是场景2" textFill="#2529e5">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值