给SpringBoot Web应用配上JavaFx漂亮衣服

1 篇文章 0 订阅
1 篇文章 0 订阅

给SpringBoot Web应用配上JavaFx漂亮衣服

提到Java开发的Web程序,第一印象就是一个黑色的窗口,里面滚动着杂乱的信息。对于服务器应用来说,无所谓这一点。如果你的应用需要发布到桌面,你就需要好好地考虑这一点了。有了JavaFX,可以帮你克服用户的恐怖心理。

SpringBoot

SpringBoot结合JavaFX,可以使得我们在JavaFx程序中采用Spring才提供的IOC、注解等特性。但是,众所周知,SpringBoot有一个较长的启动过程和启动时间,影响用户的体验。可否先展示程序的主界面,在界面加载后在运行SpringBoot。于是,最好先启动JavaFx,然后在JavaFx的初始化过程中,在另一个线程中启动SpringBoot。

上代码

// 图形模式启动
public class GuiConsoleApplication extends Application {
    private static Logger logger = LoggerFactory.getLogger(GuiConsoleApplication.class);

    private static String[] savedArgs = null;
    private static ConfigurableApplicationContext applicationContext = null;

    public static void main(String[] args) {
        savedArgs = args;
        Application.launch(GuiConsoleApplication.class, args);
    }

    public void init() throws Exception {
        CompletableFuture.supplyAsync(() -> Db2webApplication.run(savedArgs))
                .whenComplete((ctx, throwable) -> {
            if (throwable != null) {
                logger.error("Failed to load spring application context: ", throwable);
                Platform.runLater(() -> {
                    showErrorAlert(throwable);
                });
            } else {
                Platform.runLater(() -> {
                    applicationContext = ctx;
                });
            }
        });
    }

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/views/consoleView.fxml"));
        Parent root = loader.load();
        // FxMainPageController controller = loader.getController();
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("/views/consoleView.css").toExternalForm());
        stage.setTitle("数据转Web服务");

        stage.getIcons().add(new Image("/images/console.ico"));
        stage.setScene(scene);
        stage.setResizable(false);
//        stage.initStyle(StageStyle.UNDECORATED);
//        stage.setOnShown((event) -> {
//            ConsoleApplication.doUpdate(pathname);
//        });

        stage.setOnCloseRequest((event) -> System.exit(0));
        stage.show();
    }

    public static void relaunch() {
        Platform.runLater(() -> {
            try {
                // 关闭ApplicationContext
                if (applicationContext != null) {
                    applicationContext.close();
                }
                Application.launch(GuiConsoleApplication.class, savedArgs);
            } catch (Exception e) {
                 logger.error(e.getMessage());
            }
        });
    }

    private static void showErrorAlert(Throwable throwable) {
        Alert alert = new Alert(Alert.AlertType.ERROR, "Fatal error occurred.The application will exit now.Error: \n\n" + throwable.getMessage(), new ButtonType[0]);
        alert.showAndWait().ifPresent((response) -> {
            Platform.exit();
        });
    }
}

小结

不同于springboot-javafx-support技术,我们仅仅需要的是将SpringBoot Web应用表现为一个普通的本地应用程序。这里将JavaFX图形界面与SpringBoot Web服务结合在一起,且避开SpringBoot启动过程。配合Exe4j等工具,就具备了将Java Web应用作为本地应用程序部署和使用的能力,便于初级用户使用。
一般来说,这里的图形界面主要是监控和管理SpringBoot服务功能,为普通用户提供一个易操作、易控制的服务应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值