JavaFX 多个窗口(Stage)切换

JavaFX  多个窗口(Stage)切换

1.引言

    最近在做一个JavaGUI程序,考虑到JavaFX具有的MVC特性和其比Swing更丰富、便便利,故而选择了JavaFX。在使用

Java FX的过程中,最先遇到的问题就是Java FX的多个窗口切换的问题。下面是我采用的方式。

2.准备

    首先是写两个可以独立显示的界面(Main.java、Second.java)。其中Main.java 使用的FXML文件是Main.fxml;Second.java 使用的FXML文件是Second.fxml;Main.java对应的控制类为MainController.java。

    

    其中Main.fxml中仅有一个按钮,用以触发第二个页面的显示。


3.分析

    将每个界面调试完成后,在Second.java 添加方法showWindow(),在showWindow()中对Start()调用;在MainController.java中的Button事件调用方法【changeWindow()】中对Second.java的showWindow()方法调用。

4.源码
    Main.java

package test;

 

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application { 

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show(); 
    }

    public static void main(String[] args) {
        launch(args);
    }
}

    Second.java

package test;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class Second extends Application {
	 Stage stage=new Stage();

	@Override
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Second.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show(); 
    }

	public static void main(String[] args) {
		launch(args);
	}
	
	public void  showWindow() throws Exception {
		start(stage);
	}
	
}

    MainController.java

package test;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.Initializable;

public class MainController implements Initializable {

	public MainController() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub

	}
	
	public void changeWindow() throws Exception {
		test.Second second=new Second();
		second.showWindow();
		
		
	}

}

    Main.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="305.0" prefWidth="495.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.MainController">
   <children>
      <Button layoutX="182.0" layoutY="141.0" mnemonicParsing="false" onAction="#changeWindow" text="Go to Second Stage" />
      <Label layoutX="70.0" layoutY="60.0" text="MainStage" />
   </children>
</AnchorPane>

    Second.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.Second">
    <children>
        
      <Label layoutX="86.0" layoutY="77.0" text="Hello! I am Second Stage!" />
    </children>
</AnchorPane>

5.显示效果
     ---》    

6.参考博客

1.    https://blog.csdn.net/MAILLIBIN/article/details/47031505

2.    https://blog.csdn.net/legendnovo/article/details/10555941

 

  • 18
    点赞
  • 123
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
JavaFX中实现多个窗口类的窗口切换,可以通过创建多个窗口类和场景类,以及使用Stage和Scene进行窗口切换。具体步骤如下: 1. 创建多个窗口类,每个窗口类对应一个窗口界面,窗口类可以继承自javafx.scene.layout.Pane类,实现自己的布局和控件。 2. 在每个窗口类中创建一个方法,用于返回该窗口的场景对象(Scene)。在该方法中,创建一个Pane对象,将该窗口的控件添加到Pane中,并创建一个Scene对象,将Pane作为参数传递给Scene构造函数。 3. 在应用程序主类中,创建多个窗口类的对象,并将它们的场景对象(Scene)保存到一个HashMap中,其中键是窗口类的名称,值是该窗口的Scene对象。例如: ``` // 创建多个窗口类的对象 MainWindow mainWindow = new MainWindow(); SecondWindow secondWindow = new SecondWindow(); // 将它们的场景对象保存到HashMap中 Map<String, Scene> scenes = new HashMap<>(); scenes.put("MainWindow", mainWindow.getScene()); scenes.put("SecondWindow", secondWindow.getScene()); ``` 4. 在主类中创建一个方法,用于切换窗口。该方法接受一个窗口名称作为参数,并通过HashMap获取该窗口的Scene对象,然后将其设置为主舞台(Stage)的场景(Scene)。例如: ``` private void switchScene(String windowName) { Scene scene = scenes.get(windowName); primaryStage.setScene(scene); } ``` 5. 在需要切换窗口的地方,调用switchScene方法即可。例如,可以在一个按钮的事件处理方法中调用该方法: ``` switchButton.setOnAction(event -> { switchScene("SecondWindow"); }); ``` 完整的示例代码如下: MainWindow类: ``` import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; public class MainWindow extends Pane { public MainWindow() { Button switchButton = new Button("Switch to Second Window"); switchButton.setOnAction(event -> { WindowSwitcher.switchTo("SecondWindow"); }); getChildren().add(switchButton); } public Scene getScene() { return new Scene(this, 400, 300); } } ``` SecondWindow类: ``` import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.Pane; public class SecondWindow extends Pane { public SecondWindow() { Button backButton = new Button("Back to Main Window"); backButton.setOnAction(event -> { WindowSwitcher.switchTo("MainWindow"); }); getChildren().add(backButton); } public Scene getScene() { return new Scene(this, 400, 300); } } ``` WindowSwitcher类: ``` import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import java.util.HashMap; import java.util.Map; public class WindowSwitcher extends Application { private static Stage primaryStage; private static Map<String, Scene> scenes = new HashMap<>(); @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; primaryStage.setTitle("Main Window"); // 创建多个窗口类的对象 MainWindow mainWindow = new MainWindow(); SecondWindow secondWindow = new SecondWindow(); // 将它们的场景对象保存到HashMap中 scenes.put("MainWindow", mainWindow.getScene()); scenes.put("SecondWindow", secondWindow.getScene()); // 设置主舞台的场景为主窗口的场景 primaryStage.setScene(mainWindow.getScene()); primaryStage.show(); } public static void switchTo(String windowName) { Scene scene = scenes.get(windowName); primaryStage.setScene(scene); } public static void main(String[] args) { launch(args); } } ``` 在这个例子中,我们创建了MainWindow和SecondWindow两个窗口类,并在WindowSwitcher类中保存它们的场景对象。当用户点击“Switch to Second Window”按钮时,我们调用WindowSwitcher.switchTo方法切换到SecondWindow窗口。当用户点击“Back to Main Window”按钮时,我们调用WindowSwitcher.switchTo方法切换回MainWindow窗口
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值