如何在JavaFX主应用init()方法中调用预加载器的自定义方法?
实现JavaFX预加载器在初始化阶段弹出连接警告的方案
完全可以实现这个需求,核心是通过自定义预加载通知事件完成主应用与预加载器的通信——JavaFX不支持直接调用预加载器的方法,但允许主应用向预加载器发送自定义通知,由预加载器处理并弹出警告窗口。以下是具体实现步骤:
1. 创建自定义预加载通知类
这个类用来封装连接错误的具体信息,作为主应用和预加载器之间的通信载体:
public class ConnectionWarningNotification extends Preloader.PreloaderNotification { private final String warningMessage; public ConnectionWarningNotification(String message) { this.warningMessage = message; } public String getWarningMessage() { return warningMessage; } }
2. 修改主应用的init方法
在检测到HTTP响应码异常或网络错误时,通过notifyPreloader发送自定义警告通知:
public class MainApplicationLauncher extends Application { private static final int COUNT_LIMIT = 10; @Override public void init() throws Exception { // 模拟初始化进度通知 for (int i = 1; i <= COUNT_LIMIT; i++) { double progress = (double) i / COUNT_LIMIT; System.out.println("progress: " + progress); notifyPreloader(new ProgressNotification(progress)); Thread.sleep(500); } try { URL url = new URL("https://example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code = connection.getResponseCode(); System.out.println("Response code: " + code); if (code == 200) { System.out.println("连接成功!"); } else if (code == 503) { // 发送服务器维护警告 notifyPreloader(new ConnectionWarningNotification("服务器维护中,请稍后再试")); } // 可扩展处理其他响应码(如404、403等) } catch (UnknownHostException e) { // 发送网络连接失败警告 notifyPreloader(new ConnectionWarningNotification("无法连接网络,请检查网络设置")); } } @Override public void start(Stage primaryStage) throws Exception { // 初始化主应用界面 primaryStage.setTitle("主应用"); primaryStage.setScene(new Scene(new Label("应用已启动"), 300, 200)); primaryStage.show(); } public static void main(String[] args) { System.setProperty("javafx.preloader", MyPreloader.class.getCanonicalName()); Application.launch(MainApplicationLauncher.class, args); } }
3. 修改预加载器处理自定义通知
重写handleApplicationNotification方法,接收自定义警告并弹出窗口:
public class MyPreloader extends Preloader { private Stage preloaderStage; @Override public void start(Stage primaryStage) throws Exception { this.preloaderStage = primaryStage; // 初始化预加载器界面(示例用进度指示器) ProgressIndicator progressIndicator = new ProgressIndicator(); preloaderStage.setScene(new Scene(progressIndicator, 200, 200)); preloaderStage.setTitle("加载中..."); preloaderStage.show(); } @Override public void handleApplicationNotification(PreloaderNotification info) { if (info instanceof ConnectionWarningNotification) { ConnectionWarningNotification warning = (ConnectionWarningNotification) info; // 确保FX界面操作在应用线程执行 Platform.runLater(() -> { Alert alert = new Alert(Alert.AlertType.WARNING); alert.setTitle("连接警告"); alert.setHeaderText("无法连接到服务"); alert.setContentText(warning.getWarningMessage()); alert.showAndWait(); // 可选:如果需要在警告后终止应用,取消下面的注释 // Platform.exit(); }); } else if (info instanceof ProgressNotification) { // 可选:更新预加载器进度条 // ProgressNotification progress = (ProgressNotification) info; // progressIndicator.setProgress(progress.getProgress()); } } @Override public void handleStateChangeNotification(StateChangeNotification info) { // 主应用启动前关闭预加载器 if (info.getType() == StateChangeNotification.Type.BEFORE_START) { preloaderStage.hide(); } } }
关键注意点
- 所有JavaFX界面操作必须在FX应用线程执行,因此用
Platform.runLater包裹警告窗口的弹出逻辑,避免线程异常。 - 可以根据不同错误场景传递不同的警告消息,让提示内容更精准。
- 如果需要在警告后终止整个应用,可在
alert.showAndWait()后调用Platform.exit()。
内容的提问来源于stack exchange,提问作者Matias S
相关产品推荐
相关产品推荐

