利用Springboot来驱动开发桌面程序

这篇具有很好参考价值的文章主要介绍了利用Springboot来驱动开发桌面程序。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

众所周知,SpringBoot是一款强大的Javaweb开发程序,这得益于其构造了一个Spring容器,然后通过依赖注入和控制反转,维护起一套Java对象和实例的管理机制,方便开发者去使用。在web应用开发的应用中,Springboot在Java层应用非常广,同样的,也可以利用SpringBoot来编写桌面程序。

标准的JavaFx代码

JavaFx是java中比较新的桌面端应用程序开发框架,一般来说,简单的使用JavaFx编写一个桌面程序的代码如下:
下面是一个实现一个树形结构的javafx程序

package com.demo123567.desktop.auto_tools;

import com.demo123567.desktop.auto_tools.menu.FxUtils;
import com.demo123567.desktop.auto_tools.utils.DatetimeUtil;
import com.demo123567.desktop.auto_tools.utils.Json;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jcraft.jsch.*;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.converter.DefaultStringConverter;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.*;

public class SftpExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建根节点
        TreeItem<String> rootItem = new TreeItem<>("Root");

        // 创建TreeView
        TreeView<String> treeView = new TreeView<>(rootItem);

        // 创建一个按钮,用于动态添加节点
        Button addButton = new Button("Add Node");
        addButton.setOnAction(event -> {
            // 获取当前选定的节点
            TreeItem<String> selectedItem = treeView.getSelectionModel().getSelectedItem();

            // 创建一个新的子节点
            TreeItem<String> newItem = new TreeItem<>("New Node");

            // 将新节点添加到选定节点的子节点列表中
            selectedItem.getChildren().add(newItem);

            // 展开选定节点
            selectedItem.setExpanded(true);
        });

        // 创建编辑按钮,用于编辑选定节点的名称
        Button editButton = new Button("Edit Node");
        editButton.setOnAction(event -> {
            // 获取当前选定的节点
            TreeItem<String> selectedItem = treeView.getSelectionModel().getSelectedItem();

            // 如果没有选定节点,则返回
            if (selectedItem == null) {
                return;
            }

            // 创建一个对话框,用于输入新节点名称
            TextInputDialog dialog = new TextInputDialog(selectedItem.getValue());
            dialog.setTitle("Edit Node");
            dialog.setHeaderText(null);
            dialog.setContentText("Enter new node name:");

            // 显示对话框,等待用户输入
            Optional<String> result = dialog.showAndWait();

            // 如果用户输入了新名称,则将其保存到选定节点中
            result.ifPresent(name -> selectedItem.setValue(name));
        });

        // 设置单元格工厂,用于更新节点名称
        treeView.setCellFactory(TextFieldTreeCell.forTreeView());

        // 创建BorderPane,将TreeView和按钮添加到其中
        BorderPane root = new BorderPane();
        root.setCenter(treeView);

        // 创建VBox,将按钮添加到其中
        VBox buttonBox = new VBox();
        buttonBox.getChildren().addAll(addButton, editButton);
        root.setRight(buttonBox);

        // 创建场景
        Scene scene = new Scene(root, 300, 250);

        // 设置舞台标题并显示
        primaryStage.setTitle("TreeView Example");
        primaryStage.setScene(scene);
        primaryStage.show();

        // 添加监听器,在对话框关闭时输出JSON
        primaryStage.setOnCloseRequest(event -> {
            // 获取TreeView的根节点
            TreeItem<String> rootNode = treeView.getRoot();

            // 将根节点转换为Map
            Map<String,Object> ans = toMap(rootNode);

            // 输出JSON字符串
            System.out.println(Json.toJson(ans));
        });
    }
    private Map<String,Object> toMap(TreeItem<String> node) {
        Map<String,Object> ans = new HashMap<>();
        ans.put("name",node.getValue());
        if (node.getChildren().size() > 0) {
            List<Map<String,Object>> children = new ArrayList<>();
            for (TreeItem<String> child : node.getChildren()) {
                children.add(toMap(child));
            }
            ans.put("children", children);
        }

        return ans;
    }





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

运行的结构为
利用Springboot来驱动开发桌面程序

融合SpringBoot的JavaFx方法

可见,标准的启动方法为创建一个Main函数进行处理,那么我们可以联想到,如果使用Springboot,该如何启动,下面是一个完整的使用Springboot创建Javafx桌面应用的方法

springboot启动类

@SpringBootApplication
public class AutoToolsApplication {
    public static void main(String[] args) {
        Application.launch(MainApp.class, args);
    }
}

在Start函数中编写如下代码

    @Override
    public void start(Stage stage) throws Exception {
        // 创建 Spring 应用程序上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册一个配置类,以便 Spring 能够扫描和识别所有的 Bean
        context.register(Config.class);
        context.register(RestTemplateConfig2.class);
//        context.register(ThreadPoolConfig.class);
        // 启动 Spring 应用程序上下文
        context.refresh();
        stage.setTitle("效率工具");
        HostServices hostServices = getHostServices();

        MenuService functionMenuService = SpringContextUtil.getBean(MenuService.class);
        MenuBar menuBar = new MenuBar(thingMenu(functionMenuService),
                chatMenu(functionMenuService),
                browserMenu(functionMenuService,hostServices),
                logMenu(functionMenuService),
                projectMenu(functionMenuService),
                knowledgeMenu(functionMenuService),
                scriptMenu(functionMenuService),
                toolsMenu(functionMenuService),
                buttMenu(functionMenuService),
                networkToolsButton(functionMenuService),
                reminderMenu(functionMenuService),
                configurationMenu(functionMenuService),
                loveMenu(functionMenuService),
                knowledgeTreeMenu(functionMenuService,hostServices),
                sidelineMenu(functionMenuService),
                dataMenu(functionMenuService)
        );

        // 创建一个用于显示时钟的标签
        Label clockLabel = new Label();
        clockLabel.setFont(Font.font("Arial", FontWeight.BOLD, 48));

        // 创建一个用于显示"慢"字的标签
        Label slowLabel = new Label("沉心、平和、稳扎稳打");
        slowLabel.setFont(Font.font("SimSun", FontWeight.BOLD, 48));
        slowLabel.setTextFill(new Color(0f, 0f, 0f, 1));
        slowLabel.setPrefWidth(800);
        slowLabel.setAlignment(Pos.CENTER);

        StackPane clockContainer = new StackPane();
        StackPane.setAlignment(clockLabel, Pos.CENTER);
        StackPane.setAlignment(slowLabel, Pos.TOP_CENTER);

        clockContainer.getChildren().addAll(slowLabel, clockLabel);

        BorderPane.setAlignment(clockContainer, Pos.CENTER);
        BorderPane.setMargin(clockContainer, new Insets(150));
        // 创建一个用于更新时钟的时间线程
        Thread clockThread = new Thread(() -> {
            while (true) {
                Platform.runLater(() -> {
                    // 获取当前时间并设置到标签上
                    LocalDateTime currentTime = LocalDateTime.now();
                    String formattedTime = currentTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
                    clockLabel.setText(formattedTime);
                });
                try {
                    // 等待1秒钟
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        clockThread.setDaemon(true);
        clockThread.start();

        BorderPane root = new BorderPane();
        root.setTop(menuBar);
        root.setCenter(clockContainer);
        Scene scene = new Scene(root, 1920 * 0.6, 1080 * 0.6);
        stage.setScene(scene);
        stage.show();
    }

简单梳理一下这段代码,首先,利用下面的代码,创建Springboot上下文,并注册两个配置,叫Config和RestTemplateConfig2

// 创建 Spring 应用程序上下文
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        // 注册一个配置类,以便 Spring 能够扫描和识别所有的 Bean
        context.register(Config.class);
        context.register(RestTemplateConfig2.class);
//        context.register(ThreadPoolConfig.class);
        // 启动 Spring 应用程序上下文
        context.refresh();

然后在Config Bean的代码中加入@ComponentScan注解,那么整个应用的所有Bean都将被扫描并被spring上下文管理起来

@Configuration
@ComponentScan
public class Config {
}

然后,在后面的代码中,我们只需要像编写后端代码一样,编写桌面端程序即可。不需要额外学习任何的库或者技术文章来源地址https://www.toymoban.com/news/detail-445743.html

到了这里,关于利用Springboot来驱动开发桌面程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 桌面应用程序开发攻略(初步了解)

            桌面应用开发 是指为桌面计算机或其他类似设备(如服务器)开发软件应用程序的过程。桌面应用通常是独立于浏览器运行的,并且可以在操作系统的桌面或应用程序菜单中找到。桌面应用可以使用各种编程语言开发,包括C++、Java、C#和Python等。桌面应用的开发

    2024年02月09日
    浏览(34)
  • 跨端开发方案之桌面应用小程序

    小程序容器技术的未来是充满希望的,它为我们开辟了一个全新的数字世界,连接了桌面操作系统和移动生态系统之间的界限。正如技术不断演进,我们可以期待着更多的创新和发展,为用户带来更加便捷和多样化的应用体验。这一技术的推广和应用将继续推动数字科技的发

    2024年02月07日
    浏览(34)
  • C#桌面应用程序开发的学习路线

    C#桌面应用程序开发的学习路线。以下是一个基本的学习路线,供你参考: 1. C#基础 学习C#的基本语法和面向对象编程(OOP)的概念 了解C#的数据类型、变量和常量 学习控制结构(如条件语句、循环语句)和函数 2. .NET框架和Windows Forms 了解.NET框架的基本概念和架构 学习使用

    2024年02月10日
    浏览(30)
  • Vue3 Vite electron 开发桌面程序

    Electron是一个跨平台的桌面应用程序开发框架,它允许开发人员使用Web技术(如HTML、CSS和JavaScript)构建桌面应用程序,这些应用程序可以在Windows、macOS和Linux等操作系统上运行。 Electron的核心是 Chromium 浏览器内核和 Node.js 运行时环境。 Chromium 内核提供了现代浏览器的功能,

    2024年02月16日
    浏览(34)
  • 学会Python开发的第一步:写一个桌面小程序

    嗨喽,大家好呀~这里是爱看美女的茜茜呐 又到了学Python时刻~ 当使用桌面应用程序的时候,有没有那么一瞬间, 想学习一下桌面应用程序开发? 建议此次课程大家稍作了解不要浪费太多时间, 因为没有哪家公司会招聘以为Python程序员开发桌面程序吧? Python 3.6 Python是一种代

    2024年02月03日
    浏览(47)
  • 桌面应用小程序,一种创新的跨端开发方案

    Qt Group在提及2023年有桌面端应用程序开发热门趋势时,曾经提及三点: 关注用户体验:无论您是为桌面端、移动端,还是为两者一起开发应用程序,有一点是可以确定的:随着市场竞争日益激烈,对产品的期望值不断升高,终端用户的标准也在不断提高。简而言之,现在我们

    2024年02月07日
    浏览(34)
  • pywebview桌面程序开发(技术路线:前端+Python,全网独一份!!!!!!)

    官网:https://pywebview.flowrl.com/ pywebview声称Build GUI for your Python program with JavaScript, HTML, and CSS。就是可以使用web技术来实现桌面应用程序开发。其内核我理解仍然是浏览器,只不过将浏览器封装成系统窗口,这样就可以将web无缝切换到桌面应用,相比pyQt等重武器还是比较方便的

    2024年03月14日
    浏览(42)
  • 基于DirectX11+ImGui的Win32桌面程序开发

    举两个常用的开发框架,MFC和Qt Widget里面每个控件都是Window,这是和DirectUI最大的区别。下面简单梳理下这个DirectUI与GUI之前错综复杂的爱恨情仇: ① 在侏罗纪时期,传统的Handle式GUI框架,是由操作系统内核(win32k.sys)直接提供的接口(GDI),采用消息驱动的机制。窗口在Windows 操作系

    2024年02月12日
    浏览(28)
  • Vue.js + Electron 的跨平台桌面应用程序开发

    本文介绍了 Vue.js 和 Electron 的基本特点和原理,并分析了它们在桌面应用程序开发中的优势和应用场景。在基于 Vue.js 和 Electron 的桌面应用程序开发实践中,本文详细介绍了项目的搭建和配置,包括环境的准备、项目的初始化和依赖的安装等步骤。然后,本文介绍了使用 Vu

    2024年02月13日
    浏览(55)
  • 跨平台的桌面应用程序开发框架Electron | 开源日报 0906

    Stars: 109.3k License: MIT Electron 是一个基于 Node.js 和 Chromium 的开源框架,允许使用 JavaScript、HTML 和 CSS 编写跨平台的桌面应用程序。它被 Atom 编辑器等众多应用程序所采用。该项目具有以下核心优势: 跨平台:Electron 提供了 macOS、Windows 和 Linux 三个主要操作系统的二进制文件。

    2024年02月09日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包