效果展示

在这里插入图片描述
代码

package com.example.springbootdemo;import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;public class AdvancedJsonViewerGUI extends JFrame {private JTextArea inputTextArea;private JTextArea outputTextArea;private JTree jsonTree;private DefaultTreeModel treeModel;private JButton formatButton;private JButton validateButton;private JButton clearButton;private JButton copyButton;private JButton minifyButton;private JButton sampleButton;private JButton treeViewButton;private JLabel statusLabel;private JCheckBox autoFormatCheckBox;private JTabbedPane outputTabbedPane;private ObjectMapper objectMapper;public AdvancedJsonViewerGUI() {objectMapper = new ObjectMapper();initializeComponents();setupLayout();setupEventHandlers();setupWindow();}private void initializeComponents() {// 输入区域inputTextArea = new JTextArea(15, 50);inputTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));inputTextArea.setBorder(new TitledBorder("输入JSON (Input JSON)"));inputTextArea.setLineWrap(true);inputTextArea.setWrapStyleWord(true);// 输出区域 - 文本视图outputTextArea = new JTextArea(15, 50);outputTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));outputTextArea.setBorder(new TitledBorder("格式化结果 (Formatted Result)"));outputTextArea.setEditable(false);outputTextArea.setLineWrap(true);outputTextArea.setWrapStyleWord(true);// 输出区域 - 树形视图DefaultMutableTreeNode root = new DefaultMutableTreeNode("JSON");treeModel = new DefaultTreeModel(root);jsonTree = new JTree(treeModel);jsonTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);jsonTree.setShowsRootHandles(true);jsonTree.setEditable(false);jsonTree.setBorder(new TitledBorder("树形视图 (Tree View)"));// 标签页面板outputTabbedPane = new JTabbedPane();outputTabbedPane.addTab("文本视图 (Text View)", new JScrollPane(outputTextArea));outputTabbedPane.addTab("树形视图 (Tree View)", new JScrollPane(jsonTree));// 按钮formatButton = new JButton("格式化 (Format)");validateButton = new JButton("验证 (Validate)");clearButton = new JButton("清空 (Clear)");copyButton = new JButton("复制结果 (Copy)");minifyButton = new JButton("压缩 (Minify)");sampleButton = new JButton("示例 (Sample)");treeViewButton = new JButton("生成树 (Build Tree)");// 复选框autoFormatCheckBox = new JCheckBox("自动格式化 (Auto Format)", false);// 状态标签statusLabel = new JLabel("就绪 (Ready)");statusLabel.setForeground(Color.BLUE);}private void setupLayout() {setLayout(new BorderLayout());// 顶部面板 - 按钮区域JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));topPanel.add(formatButton);topPanel.add(validateButton);topPanel.add(minifyButton);topPanel.add(copyButton);topPanel.add(clearButton);topPanel.add(sampleButton);topPanel.add(treeViewButton);topPanel.add(autoFormatCheckBox);// 中部面板 - 文本区域JPanel textPanel = new JPanel(new GridLayout(1, 2, 10, 0));JPanel inputPanel = new JPanel(new BorderLayout());inputPanel.add(new JScrollPane(inputTextArea), BorderLayout.CENTER);textPanel.add(inputPanel);textPanel.add(outputTabbedPane);// 底部状态栏JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));statusPanel.add(statusLabel);add(topPanel, BorderLayout.NORTH);add(textPanel, BorderLayout.CENTER);add(statusPanel, BorderLayout.SOUTH);}private void setupEventHandlers() {// 格式化按钮formatButton.addActionListener(e -> formatJson());// 验证按钮validateButton.addActionListener(e -> validateJson());// 清空按钮clearButton.addActionListener(e -> clearAll());// 复制按钮copyButton.addActionListener(e -> copyToClipboard());// 压缩按钮minifyButton.addActionListener(e -> minifyJson());// 示例按钮sampleButton.addActionListener(e -> loadSample());// 树形视图按钮treeViewButton.addActionListener(e -> buildTreeView());// 自动格式化autoFormatCheckBox.addActionListener(e -> {if (autoFormatCheckBox.isSelected()) {inputTextArea.addKeyListener(new KeyAdapter() {@Overridepublic void keyReleased(KeyEvent e) {// 延迟执行格式化,避免频繁触发Timer timer = new Timer(500, ev -> formatJson());timer.setRepeats(false);timer.start();}});showStatus("已启用自动格式化", Color.GREEN);}});// 输入区域按键监听inputTextArea.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {// Ctrl+Enter 格式化if (e.getKeyCode() == KeyEvent.VK_ENTER && e.isControlDown()) {formatJson();}}});}private void setupWindow() {setTitle("高级JSON查看器 - Advanced JSON Viewer");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pack();setLocationRelativeTo(null);setResizable(true);setMinimumSize(new Dimension(900, 700));}private void formatJson() {String input = inputTextArea.getText().trim();if (input.isEmpty()) {showStatus("请输入JSON内容", Color.RED);return;}try {JsonNode jsonNode = objectMapper.readTree(input);String formattedJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);outputTextArea.setText(formattedJson);showStatus("格式化成功", Color.GREEN);} catch (Exception e) {outputTextArea.setText("错误: 无效的JSON格式\n\n" + e.getMessage());showStatus("格式化失败: " + e.getMessage(), Color.RED);}}private void minifyJson() {String input = inputTextArea.getText().trim();if (input.isEmpty()) {showStatus("请输入JSON内容", Color.RED);return;}try {JsonNode jsonNode = objectMapper.readTree(input);String minifiedJson = objectMapper.writeValueAsString(jsonNode);outputTextArea.setText(minifiedJson);showStatus("压缩成功", Color.GREEN);} catch (Exception e) {outputTextArea.setText("错误: 无效的JSON格式\n\n" + e.getMessage());showStatus("压缩失败: " + e.getMessage(), Color.RED);}}private void validateJson() {String input = inputTextArea.getText().trim();if (input.isEmpty()) {showStatus("请输入JSON内容", Color.RED);return;}try {objectMapper.readTree(input);showStatus("✓ 有效的JSON格式", Color.GREEN);JOptionPane.showMessageDialog(this, "✓ 有效的JSON格式", "验证结果", JOptionPane.INFORMATION_MESSAGE);} catch (Exception e) {showStatus("✗ 无效的JSON格式", Color.RED);JOptionPane.showMessageDialog(this, "✗ 无效的JSON格式:\n" + e.getMessage(), "验证结果", JOptionPane.ERROR_MESSAGE);}}private void copyToClipboard() {String output = outputTextArea.getText();if (!output.isEmpty()) {StringSelection stringSelection = new StringSelection(output);Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();clipboard.setContents(stringSelection, null);showStatus("结果已复制到剪贴板", Color.GREEN);} else {showStatus("没有内容可复制", Color.RED);}}private void clearAll() {inputTextArea.setText("");outputTextArea.setText("");treeModel.setRoot(new DefaultMutableTreeNode("JSON"));showStatus("已清空", Color.BLUE);}private void loadSample() {String sampleJson = "{\n" +"  \"name\": \"张三\",\n" +"  \"age\": 30,\n" +"  \"email\": \"zhangsan@example.com\",\n" +"  \"address\": {\n" +"    \"street\": \"长安街100号\",\n" +"    \"city\": \"北京\",\n" +"    \"zipcode\": \"100000\"\n" +"  },\n" +"  \"skills\": [\"Java\", \"Python\", \"JavaScript\"],\n" +"  \"married\": true,\n" +"  \"children\": null,\n" +"  \"projects\": [\n" +"    {\n" +"      \"name\": \"项目A\",\n" +"      \"duration\": 12,\n" +"      \"technologies\": [\"Spring\", \"React\"]\n" +"    },\n" +"    {\n" +"      \"name\": \"项目B\",\n" +"      \"duration\": 8,\n" +"      \"technologies\": [\"Node.js\", \"Vue.js\"]\n" +"    }\n" +"  ]\n" +"}";inputTextArea.setText(sampleJson);showStatus("已加载示例数据", Color.BLUE);}private void buildTreeView() {String input = inputTextArea.getText().trim();if (input.isEmpty()) {showStatus("请输入JSON内容", Color.RED);return;}try {JsonNode jsonNode = objectMapper.readTree(input);DefaultMutableTreeNode root = new DefaultMutableTreeNode("JSON");buildTreeNodes(jsonNode, root, "root");treeModel.setRoot(root);outputTabbedPane.setSelectedIndex(1); // 切换到树形视图showStatus("树形视图构建成功", Color.GREEN);} catch (Exception e) {showStatus("构建树形视图失败: " + e.getMessage(), Color.RED);}}private void buildTreeNodes(JsonNode jsonNode, DefaultMutableTreeNode parent, String fieldName) {if (jsonNode.isObject()) {DefaultMutableTreeNode objectNode = new DefaultMutableTreeNode(fieldName + " (Object)");parent.add(objectNode);jsonNode.fields().forEachRemaining(entry -> {String key = entry.getKey();JsonNode value = entry.getValue();buildTreeNodes(value, objectNode, key);});} else if (jsonNode.isArray()) {DefaultMutableTreeNode arrayNode = new DefaultMutableTreeNode(fieldName + " (Array[" + jsonNode.size() + "])");parent.add(arrayNode);for (int i = 0; i < jsonNode.size(); i++) {JsonNode element = jsonNode.get(i);buildTreeNodes(element, arrayNode, "[" + i + "]");}} else {String valueStr = jsonNode.asText();if (jsonNode.isNull()) {valueStr = "null";} else if (jsonNode.isNumber()) {valueStr = jsonNode.asText();} else if (jsonNode.isBoolean()) {valueStr = String.valueOf(jsonNode.asBoolean());} else {valueStr = "\"" + valueStr + "\"";}parent.add(new DefaultMutableTreeNode(fieldName + ": " + valueStr));}}private void showStatus(String message, Color color) {statusLabel.setText(message);statusLabel.setForeground(color);}public static void main(String[] args) {try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception e) {e.printStackTrace();}SwingUtilities.invokeLater(() -> {new AdvancedJsonViewerGUI().setVisible(true);});}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/92921.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/92921.shtml
英文地址,请注明出处:http://en.pswp.cn/pingmian/92921.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

真实案例 | 如何用iFlyCode开发Webpack插件?

01案例背景在项目中&#xff0c;我们经常需要存档前端每次打包的版本&#xff0c;方便线上快速切换不同版本使用。经过思考&#xff0c;我们可以在打包时&#xff0c;将本次打包结果另存为zip压缩包&#xff0c;方便后续使用。于是我准备开发一个Webpack插件实现此功能&#xf…

19day-人工智能-机器学习-分类算法-决策树

1. 什么是决策树学过数据结构与算法的小伙伴应该对树不陌生吧&#xff0c;这里的决策树也是大同小异的&#xff0c;只是每次反之都有一个条件来决定流向的。1.1 决策节点通过条件判断而进行分支选择的节点。如&#xff1a;将某个样本中的属性值(特征值)与决策节点上的值进行比较…

地球磁层全球MHD模型中模拟Dst指数的半经验方法

A semi-empirical approach to simulating the Dst index in global MHD models of Earth’s magnetosphere pdf 1 Introduction Dst指数 (Disturbance storm time index, 地磁暴时扰动指数) 是描述磁暴活动强度应用最广泛的指数&#xff0c;对于研究地磁扰动和磁暴具有重要意…

什么是脏读、幻读、不可重复读?

脏读、幻读和不可重复读是数据库事务隔离级别中常见的三种数据一致性问题。它们描述了在并发事务环境下可能出现的异常现象。下面通过对比表格和具体示例进行清晰解析&#xff1a;核心概念对比表问题类型触发场景本质原因示例脏读 (Dirty Read)事务A读取了事务B未提交的修改读取…

腾讯位置商业授权微信小程序关键词输入提示

微信小程序JavaScript SDK 开发指南 关键词输入提示 getSuggestion(options:Object) 用于获取输入关键字的补完与提示&#xff0c;帮助用户快速输入 注&#xff1a;坐标系采用gcj02坐标系 options属性说明 属性类型必填说明keywordString是用户输入的关键词&#xff08;希望…

LabVIEW菜单操控

该程序围绕运行时菜单栏操作&#xff0c;实现从初始化构建菜单结构&#xff08;含菜单项、快捷键 &#xff09;&#xff0c;到响应交互删除特定菜单项&#xff0c;再到监控界面事件驱动逻辑&#xff0c;完成自定义菜单交互全流程&#xff0c;适配需灵活菜单控制的程序开发场景。…

Web 服务详解:HTTP 与 HTTPS 配置

Web 服务详解&#xff1a;HTTP 与 HTTPS 配置 一、HTTP 服务概述 HTTP&#xff08;Hypertext Transfer Protocol&#xff0c;超文本传输协议&#xff09;是用于在网络上传输网页数据的基础协议&#xff0c;默认使用80 端口&#xff0c;以明文形式传输数据。常见的 HTTP 服务软…

YOLO-v2-tiny 20种物体检测模型

一、简介 YOLO-v2-tiny是基于YOLO(You Only Look Once)实时目标检测算法的轻量级版本&#xff0c;专门为嵌入式设备和资源受限环境优化。本模型能够检测20种常见物体类别&#xff0c;在保持较高检测精度的同时大幅减少了计算量和模型大小。 20种物体检测模型&#xff0c; 使用…

heterophilic graph和hetergeneous graph区别(附带homophilic graph 和homoegeneous graph)

Heterophilic Graph&#xff08;异配图&#xff09;连接的节点在属性上不相似,但是所有节点和边的类别都是同一种类型&#xff0c;数据集如squirrel / chameleon&#xff0c;它们是 heterogeneous graph&#xff08;异质图&#xff09;而不是Heterophilic Graph&#xff08;异配…

Thinkphp(GUI)漏洞利用工具,支持各版本TP漏洞检测,命令执行,Getshell

工具介绍 Thinkphp(GUI)漏洞利用工具&#xff0c;支持各版本TP漏洞检测&#xff0c;命令执行&#xff0c;Getshell。JAVAFX可视化编写&#xff0c;博主第一次用javafx来写界面&#xff0c;第一次学习尝试&#xff0c;仅仅只用于学习尝试如果缺少什么payload&#xff0c;欢迎提交…

GitHub分支保护介绍(Branch Protection)(git分支保护)(通过设置规则和权限来限制对特定分支的操作的功能)

文章目录**1. 核心功能****a. 防止误操作****b. 强制代码审查****c. 状态检查&#xff08;Status Checks&#xff09;****d. 权限控制****2. 如何设置分支保护&#xff1f;**1. **进入仓库设置**2. **添加分支保护规则**3. **配置保护规则**4. **保存设置****3. 常见应用场景**…

怎么理解On-Premises

On-Premises 指的是—— 软件、系统、数据中心等部署并运行在企业自己管理的本地硬件或机房里&#xff0c;而不是放在云端或第三方托管环境中。 你可以把它理解成&#xff1a;“服务器在你自己家里&#xff08;公司机房&#xff09;&#xff0c;而不是寄放在别人家&#xff08;…

UserController类讲解

用户管理控制器&#xff0c;实现了用户CRUD操作的RESTful API&#xff1a; 1. 类结构与核心注解 1.1 控制器声明 RestController RequestMapping("/api/users") public class UserControllerRestController 深度解析&#xff1a; 组合注解&#xff1a;Controller Re…

【剑指offer】搜索算法

目录 &#x1f4c1; JZ53 数字在升序数组中出现的次数​编辑 &#x1f4c1; JZ4 二维数组中的查找​编辑 &#x1f4c1; JZ11 旋转数组的最小数字 &#x1f4c1; JZ38 字符串的排列​编辑 &#x1f4c1; JZ53 数字在升序数组中出现的次数 这就是一道简单的模板题&#xff0…

ETLCloud批流一体化体现在哪

ETLCloud批流一体化体现在哪 企业对数据处理的实时性、高效性和准确性的要求越来越高。批流一体化作为一种先进的数据处理理念&#xff0c;逐渐被企业所采用。 目前许多国产化ETL工具也装配了十分强大的批流一体化能力&#xff0c;ETLCoud就是一个很好的代表&#xff0c;它能够…

Mybatis学习之缓存(九)

这里写目录标题一、MyBatis的一级缓存1.1、工作原理1.2、一级缓存失效的四种情况1.3、不同的SqlSession对应不同的一级缓存1.4、同一个SqlSession但是查询条件不同1.5、同一个SqlSession两次查询期间执行了任何一次增删改操作1.6、同一个SqlSession两次查询期间手动清空了&…

windows10装Ubuntu22.04系统(双系统)

参考链接&#xff1a;Windows和Linux双系统的保姆级安装教程&#xff0c;新手小白跟着也能装_windows安装linux双系统-CSDN博客 1 前期准备 1.下载Ubuntu22.04.5 的iso镜像文件&#xff1a;Download Ubuntu Desktop | Ubuntu 2.准备一个U盘&#xff08;空&#xff0c;已有文…

Pandas数据处理与分析实战:Pandas数据清洗与处理入门

数据清洗&#xff1a;Pandas数据处理入门 学习目标 本课程将引导学员了解数据清洗的基本概念&#xff0c;掌握使用Pandas库处理数据集中的缺失值、重复数据和异常值的方法&#xff0c;确保数据的质量&#xff0c;为后续的数据分析和机器学习任务打下坚实的基础。 相关知识点 Pa…

Python爬虫实战:研究ScrapyRT框架,构建图书商城数据采集系统

1. 引言 1.1 研究背景 在当今数字化时代,互联网已成为全球最大的信息库,蕴含着海量的有价值数据,涵盖商业、教育、科研、医疗等各个领域。根据 IDC(国际数据公司)预测,到 2025 年全球数据圈将增长至 175ZB,其中网络数据占比超过 60%。这些数据不仅是企业制定商业策略、…

springboot接口请求参数校验

参数校验 参数校验可以防止无效或错误的数据进入系统。通过校验前端输入的参数&#xff0c;可以确保数据的完整性&#xff0c;避免因为缺少必要的信息而导致程序错误或异常。例如&#xff0c;对于密码字段&#xff0c;可以通过校验规则要求用户输入至少8个字符、包含字母和数字…