Java FX多窗口编程
立即下载
资源介绍:
核心点:在MainClass初始化窗口时,将MainClass的引用传给相应窗口的控制器。之后在每个窗口的控制器中通过MainClass的引用完成从窗口的打开以及主从窗口的通信。
package com.example.multiwindown;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class MainClass extends Application {
//主窗口
private Stage primaryStage;
//主控制器
private PrimaryController primaryController;
//start阶段进行初始化
@Override
public void start(Stage stage) throws IOException {
primaryStage = stage;
this.showPrimaryStage();
}
public void showPrimaryStage() {
FXMLLoader fxmlLoader = new FXMLLoader(MainClass.class.getResource("primaryView.fxml"));
Scene scene = null;
try {
scene = new Scene(fxmlLoader.load(), 800, 600);
} catch (IOException e) {
e.printStackTrace();
}
PrimaryController controller = fxmlLoader.getController();
//-------------------------------------------------
controller.setMain(this);
//-------------------------------------------------
this.primaryController = controller;
primaryStage.setTitle("Hello!");
primaryStage.setScene(scene);
primaryStage.show();
}
//打开新窗口
public void openNewStage() {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newStageView1.fxml"));
Scene scene = new Scene(fxmlLoader.load());
NewStageController controller = fxmlLoader.getController();
Stage newStage = new Stage();
newStage.setScene(scene);
newStage.setTitle("新的窗体");
//------------------------------------------------
controller.setMain(this, newStage);
//------------------------------------------------
newStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
//切换主窗口的Scene
public void changePrimaryScene() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("newSceneView.fxml"));
Scene scene = new Scene(loader.load());
NewSceneController controller = loader.getController();
//-------------------------------------------
controller.setMain(this);
//---------------------------------------
primaryStage.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
//打开展示信息窗口
public void openSendStage(String msg) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("showMsgView.fxml"));
Scene scene = new Scene(fxmlLoader.load());
Stage stage = new Stage();
ShowMsgController controller1 = fxmlLoader.getController();
//---------------------------------------------
controller1.init(this, stage);
//----------------------------------------------
controller1.showMessage(msg);
stage.setScene(scene);
stage.setTitle("show msg window");
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
//打开发送窗口
public void openReceiveStage() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("receiveMsgView.fxml"));
Scene scene = new Scene(loader.load());
Stage stage = new Stage();
ReceiveMsgController controller = loader.getController();
//-------------------------------------------------------
controller.init(this.primaryController, stage);
//-------------------------------------------------------
stage.setScene(scene);
stage.setTitle("send Message");
stage.show();
}
public static void main(String[] args) {
launch();
}
}