工作之余写了一个转换小工具,具有以下功能:
package org.binbin.container.panel;
import javax.swing.*;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/9/28 14:20
*/
public class TimePanel extends JPanel {
private static final String template = "当前时间:TIME 当前时间戳:STAMP";
private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private ExecutorService pool = Executors.newSingleThreadExecutor();
private boolean stop;
private JTextField currentTimeField;
private JButton stopButton;
private JTextField[] textFields;
private JButton convertBuffon1;
private JButton convertBuffon2;
private JComboBox<String> comboBox1;
private JComboBox<String> comboBox2;
public TimePanel() {
currentTimeField = new JTextField(template);
currentTimeField.setToolTipText("请暂停时间后再复制");
currentTimeField.setEditable(false);
currentTimeField.setBorder(BorderFactory.createEmptyBorder());
stopButton = new JButton("暂停");
stopButton.addActionListener(e -> {
stop = !stop;
stopButton.setText(stop ? "继续" : "暂停");
if(!stop) {
pool.submit(new TimeDriver());
}
});
pool.submit(new TimeDriver());
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.add(currentTimeField);
panel1.add(stopButton);
setLayout(new FlowLayout());
add(panel1);
textFields = new JTextField[4];
for (int i = 0; i < textFields.length; i++) {
textFields[i] = new JTextField(12);
}
convertBuffon1 = new JButton("转换 >");
convertBuffon2 = new JButton("转换 >");
convertBuffon1.addActionListener(e -> {
try {
long time = Long.parseLong(textFields[0].getText());
int index = comboBox1.getSelectedIndex();
if(index == 1) {
time *= 1000;
}
Date date = new Date();
date.setTime(time);
textFields[1].setText(df.format(date));
} catch (Exception ex) {
ex.printStackTrace();
}
});
convertBuffon2.addActionListener(e -> {
try {
Date date = df.parse(textFields[2].getText());
int index = comboBox2.getSelectedIndex();
long time = date.getTime();
if(index == 1) {
time /= 1000;
}
textFields[3].setText(time + "");
} catch (Exception ex) {
ex.printStackTrace();
}
});
comboBox1 = new JComboBox<>();
comboBox2 = new JComboBox<>();
comboBox1.addItem("毫秒(ms)");
comboBox1.addItem("秒(s)");
comboBox2.addItem("毫秒(ms)");
comboBox2.addItem("秒(s)");
JPanel panel2 = new JPanel();
panel2.add(textFields[0]);
panel2.add(comboBox1);
panel2.add(convertBuffon1);
panel2.add(textFields[1]);
JPanel panel3 = new JPanel();
panel3.add(textFields[2]);
panel3.add(convertBuffon2);
panel3.add(textFields[3]);
panel3.add(comboBox2);
init();
add(panel2);
add(panel3);
}
public void init() {
Date date = new Date();
textFields[0].setText(date.getTime() + "");
textFields[2].setText(df.format(date));
}
class TimeDriver implements Runnable {
private final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void run() {
while (!stop) {
try {
Date now = new Date();
String text = template.replace("TIME", df.format(now)).replace("STAMP", now.getTime() + "");
currentTimeField.setText(text);
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
package org.binbin.container.panel;
import javax.swing.*;
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.util.Base64;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/10/27 11:30
*/
public class Base64Panel extends JPanel implements ActionListener {
private JButton[] buttons;
private String[] buttonNames = {"编码", "解码", "复制", "清空"};
private JTextArea sourceTextArea;
private JTextArea targetTextArea;
public Base64Panel() {
setLayout(new BorderLayout());
sourceTextArea = new JTextArea(10, 20);
targetTextArea = new JTextArea(10, 20);
sourceTextArea.setLineWrap(true);
targetTextArea.setLineWrap(true);
sourceTextArea.setMargin(new Insets(10, 10, 10, 10));
targetTextArea.setMargin(new Insets(10, 10, 10, 10));
targetTextArea.setEditable(false);
Box box=Box.createHorizontalBox();
box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
box.add(new JScrollPane(sourceTextArea));
box.add(Box.createHorizontalStrut(10));
box.add(new JLabel("==>", JLabel.CENTER));
box.add(Box.createHorizontalStrut(10));
box.add(new JScrollPane(targetTextArea));
add(box, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttons = new JButton[buttonNames.length];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(buttonNames[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "编码":
try {
String text = sourceTextArea.getText();
String result = Base64.getEncoder().encodeToString(text.getBytes("UTF-8"));
targetTextArea.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "解码":
try {
String text = sourceTextArea.getText();
String result = new String(Base64.getDecoder().decode(text), "UTF-8");
targetTextArea.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "复制":
try {
String text = targetTextArea.getText();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(text), null);
JOptionPane.showMessageDialog(this, "复制成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "清空":
sourceTextArea.setText("");
targetTextArea.setText("");
break;
}
}
}
package org.binbin.container.panel;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionListener;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/10/28 11:30
*/
public class UrlPanel extends JPanel {
private JButton[] buttons;
private String[] buttonNames = {"编码", "解码", "复制", "清空"};
private JTextArea sourceTextArea;
private JTextArea targetTextArea;
public UrlPanel() {
setLayout(new BorderLayout());
Box box=Box.createHorizontalBox();
box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
sourceTextArea = new JTextArea(10, 20);
targetTextArea = new JTextArea(10, 20);
sourceTextArea.setLineWrap(true);
targetTextArea.setLineWrap(true);
sourceTextArea.setMargin(new Insets(10, 10, 10, 10));
targetTextArea.setMargin(new Insets(10, 10, 10, 10));
targetTextArea.setEditable(false);
box.add(new JScrollPane(sourceTextArea));
box.add(Box.createHorizontalStrut(10));
box.add(new JLabel("==>", JLabel.CENTER));
box.add(Box.createHorizontalStrut(10));
box.add(new JScrollPane(targetTextArea));
add(box, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttons = new JButton[buttonNames.length];
ActionListener listener = e -> {
switch (e.getActionCommand()) {
case "编码":
try {
String text = sourceTextArea.getText();
String result = URLEncoder.encode(text, "UTF-8");
targetTextArea.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "解码":
try {
String text = sourceTextArea.getText();
String result = URLDecoder.decode(text, "UTF-8");
targetTextArea.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "复制":
try {
String text = targetTextArea.getText();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(text), null);
JOptionPane.showMessageDialog(this, "复制成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "清空":
sourceTextArea.setText("");
targetTextArea.setText("");
break;
}
};
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(buttonNames[i]);
buttons[i].addActionListener(listener);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.SOUTH);
}
}
package org.binbin.container.panel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSONObject;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/11/1 14:38
*/
public class JsonPanel extends JPanel implements ActionListener, DocumentListener {
private JButton[] buttons;
private String[] buttonNames = {"复制", "清空"};
private JTextArea sourceTextArea;
private JTextArea targetTextArea;
public JsonPanel() {
setLayout(new BorderLayout());
sourceTextArea = new JTextArea(10, 20);
targetTextArea = new JTextArea(10, 20);
sourceTextArea.setLineWrap(true);
targetTextArea.setLineWrap(true);
sourceTextArea.setMargin(new Insets(10, 10, 10, 10));
targetTextArea.setMargin(new Insets(10, 10, 10, 10));
sourceTextArea.setTabSize(2);
targetTextArea.setTabSize(2);
targetTextArea.setEditable(false);
Box box=Box.createHorizontalBox();
box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
box.add(new JScrollPane(sourceTextArea));
box.add(Box.createHorizontalStrut(10));
box.add(new JLabel("==>", JLabel.CENTER));
box.add(Box.createHorizontalStrut(10));
box.add(new JScrollPane(targetTextArea));
add(box, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttons = new JButton[buttonNames.length];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(buttonNames[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.SOUTH);
sourceTextArea.getDocument().addDocumentListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "复制":
try {
String text = targetTextArea.getText();
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(text), null);
JOptionPane.showMessageDialog(this, "复制成功!", "结果", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case "清空":
sourceTextArea.setText("");
targetTextArea.setText("");
break;
}
}
private void format() {
try {
String text = sourceTextArea.getText();
if(text == null || text.trim().equals("")) {
return;
}
JSONObject object = JSONObject.parseObject(text);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
targetTextArea.setText(pretty);
} catch (Exception ex) {
targetTextArea.setText("");
}
}
@Override
public void insertUpdate(DocumentEvent documentEvent) {
format();
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
format();
}
@Override
public void changedUpdate(DocumentEvent documentEvent) {
format();
}
}
package org.binbin.util;
import javax.swing.*;
import java.awt.*;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/10/28 11:05
*/
public class FrameUtil {
public static void center(JFrame frame) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2);
}
}
package org.binbin.container;
import org.binbin.container.panel.Base64Panel;
import org.binbin.container.panel.JsonPanel;
import org.binbin.container.panel.TimePanel;
import org.binbin.container.panel.UrlPanel;
import org.binbin.util.FrameUtil;
import javax.swing.*;
import java.awt.event.KeyEvent;
/**
*
* Copyright © 启明星辰 版权所有
*
* @author 胡海斌/[email protected]
* 2023/10/28 10:59
*/
public class MainFrame extends JFrame {
private static final String[] titles = new String[]{"时间戳转换", "Base64编码/解码", "URL编码/解码", "JSON格式化"};
private JTabbedPane tabbedPane;
private JPanel base64Panel;
private JPanel urlPanel;
private TimePanel timePanel;
private JsonPanel jsonPanel;
public MainFrame() {
setTitle("工具箱 v1.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
FrameUtil.center(this);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu featureMenu = new JMenu("功能(F)");
featureMenu.setMnemonic(KeyEvent.VK_F);
for (int i = 0; i < titles.length; i++) {
JMenuItem item = new JMenuItem(titles[i]);
final int index = i;
item.addActionListener(e -> {
if(tabbedPane.getSelectedIndex() == 0) {
timePanel.init();
}
tabbedPane.setSelectedIndex(index);
});
featureMenu.add(item);
}
featureMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("退出(E)");
exitItem.setMnemonic(KeyEvent.VK_E);
exitItem.addActionListener(e -> {
System.exit(0);
});
featureMenu.add(exitItem);
bar.add(featureMenu);
JMenu helpMenu = new JMenu("帮助(H)");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem aboutItem = new JMenuItem("关于(A)");
aboutItem.setMnemonic(KeyEvent.VK_A);
aboutItem.addActionListener(e -> {
String msg = "工具箱 v1.0
作者:斌斌
邮箱:[email protected]
日期:2023/09/28
欢迎您使用本工具箱!如果有什么建议,请给作者发邮件。
";
JOptionPane.showMessageDialog(this, msg, "关于", JOptionPane.INFORMATION_MESSAGE);
});
helpMenu.add(aboutItem);
bar.add(helpMenu);
tabbedPane = new JTabbedPane();
add(tabbedPane);
base64Panel = new Base64Panel();
urlPanel = new UrlPanel();
timePanel = new TimePanel();
jsonPanel = new JsonPanel();
tabbedPane.add(titles[0], timePanel);
tabbedPane.add(titles[1], base64Panel);
tabbedPane.add(titles[2], urlPanel);
tabbedPane.add(titles[3], jsonPanel);
tabbedPane.addChangeListener(e -> {
if(tabbedPane.getSelectedIndex() == 2) {
timePanel.init();
}
});
}
}
package org.binbin;
import org.binbin.container.MainFrame;
import java.awt.*;
/**
* 小工具
* @author binbin
* 2023/10/27 11:30
*/
public class App
{
public static void main( String[] args )
{
EventQueue.invokeLater(() -> {
new MainFrame().setVisible(true);
});
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.hbingroupId>
<artifactId>toolartifactId>
<version>1.0-SNAPSHOTversion>
<name>infoname>
<url>www.binbin.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.32version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>3.2.0version>
<configuration>
<archive>
<manifest>
<mainClass>org.binbin.AppmainClass>
manifest>
archive>
<descriptorRefs>
<descriptorRef>jar-with-dependenciesdescriptorRef>
descriptorRefs>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
<version>2.10.4version>
<configuration>
<encoding>UTF-8encoding>
<aggregate>trueaggregate>
<charset>UTF-8charset>
<docencoding>UTF-8docencoding>
configuration>
<executions>
<execution>
<id>attach-javadocsid>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-source-pluginartifactId>
<version>3.0.1version>
<executions>
<execution>
<id>attach-sourcesid>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>