In my spare time, I wrote a conversion gadget with the following functions:
- timestamp conversion
- Base64 encoding/decoding
- URL encoding/decoding
- JSON formatting
Time stamp conversion
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 ? All Rights Reserved by VenusChen * * @author Hu Haibin/[email protected] * 2023/9/28 14:20 */ public class TimePanel extends JPanel {<!-- --> private static final String template = "Current time: TIME Current timestamp: 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("Please pause for a while before copying"); currentTimeField.setEditable(false); currentTimeField.setBorder(BorderFactory.createEmptyBorder()); stopButton = new JButton("Pause"); stopButton.addActionListener(e -> {<!-- --> stop = !stop; stopButton.setText(stop ? "Continue" : "Pause"); 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("Convert >"); convertBuffon2 = new JButton("Convert >"); 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("Milliseconds(ms)"); comboBox1.addItem("seconds(s)"); comboBox2.addItem("Milliseconds(ms)"); comboBox2.addItem("seconds(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(); } } } } }
Base64 encoding/decoding
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 ? All Rights Reserved by VenusChen * * @author Hu Haibin/[email protected] * 2023/10/27 11:30 */ public class Base64Panel extends JPanel implements ActionListener {<!-- --> private JButton[] buttons; private String[] buttonNames = {<!-- -->"Encoding", "Decoding", "Copy", "Clear"}; 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 "encoding": try {<!-- --> String text = sourceTextArea.getText(); String result = Base64.getEncoder().encodeToString(text.getBytes("UTF-8")); targetTextArea.setText(result); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "decoding": 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 "copy": try {<!-- --> String text = targetTextArea.getText(); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(text), null); JOptionPane.showMessageDialog(this, "Copy successfully!", "Result", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "clear": sourceTextArea.setText(""); targetTextArea.setText(""); break; } } }
URL encoding/decoding
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 ? All Rights Reserved by VenusChen * * @author Hu Haibin/[email protected] * 2023/10/28 11:30 */ public class UrlPanel extends JPanel {<!-- --> private JButton[] buttons; private String[] buttonNames = {<!-- -->"Encoding", "Decoding", "Copy", "Clear"}; 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 "encoding": try {<!-- --> String text = sourceTextArea.getText(); String result = URLEncoder.encode(text, "UTF-8"); targetTextArea.setText(result); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "decoding": try {<!-- --> String text = sourceTextArea.getText(); String result = URLDecoder.decode(text, "UTF-8"); targetTextArea.setText(result); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "copy": try {<!-- --> String text = targetTextArea.getText(); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(text), null); JOptionPane.showMessageDialog(this, "Copy successfully!", "Result", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "clear": 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); } }
JSON formatting
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 ? All Rights Reserved by VenusChen * * @author Hu Haibin/[email protected] * 2023/11/1 14:38 */ public class JsonPanel extends JPanel implements ActionListener, DocumentListener {<!-- --> private JButton[] buttons; private String[] buttonNames = {<!-- -->"Copy", "Clear"}; 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 "copy": try {<!-- --> String text = targetTextArea.getText(); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(new StringSelection(text), null); JOptionPane.showMessageDialog(this, "Copy successfully!", "Result", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) {<!-- --> ex.printStackTrace(); } break; case "clear": 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(); } }
Tools
package org.binbin.util; import javax.swing.*; import java.awt.*; /** * *Copyright ? All Rights Reserved by VenusChen * * @author Hu Haibin/[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); } }
Main form
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 ? All Rights Reserved by VenusChen * * @author Hu Haibin/[email protected] * 2023/10/28 10:59 */ public class MainFrame extends JFrame {<!-- --> private static final String[] titles = new String[]{<!-- -->"Timestamp conversion", "Base64 encoding/decoding", "URL encoding/decoding", "JSON format change"}; private JTabbedPane tabbedPane; private JPanel base64Panel; private JPanel urlPanel; private TimePanel timePanel; private JsonPanel jsonPanel; public MainFrame() {<!-- --> setTitle("Toolbox v1.0"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); FrameUtil.center(this); JMenuBar bar = new JMenuBar(); setJMenuBar(bar); JMenu featureMenu = new JMenu("Function(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("Exit(E)"); exitItem.setMnemonic(KeyEvent.VK_E); exitItem.addActionListener(e -> {<!-- --> System.exit(0); }); featureMenu.add(exitItem); bar.add(featureMenu); JMenu helpMenu = new JMenu("Help(H)"); helpMenu.setMnemonic(KeyEvent.VK_H); JMenuItem aboutItem = new JMenuItem("About (A)"); aboutItem.setMnemonic(KeyEvent.VK_A); aboutItem.addActionListener(e -> {<!-- --> String msg = "<html><body><p style='font-weight:bold;font-size:14px;'>Toolbox v1.0</p><br/><p style='font-weight:normal'>Author: Bin Bin<br/>Email: [email protected]<br/>Date: 2023/09/28<br /><br/>Welcome to use this toolbox! If you have any suggestions, please email the author.</p></body></html>"; JOptionPane.showMessageDialog(this, msg, "About", 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(); } }); } }
Startup class
package org.binbin; import org.binbin.container.MainFrame; import java.awt.*; /** * small tools * @author binbin * 2023/10/27 11:30 */ public class App {<!-- --> public static void main( String[] args ) {<!-- --> EventQueue.invokeLater(() -> {<!-- --> new MainFrame().setVisible(true); }); } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>org.hbin</groupId> <artifactId>tool</artifactId> <version>1.0-SNAPSHOT</version> <name>info</name> <url>www.binbin.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.32</version> </dependency> </dependencies> <build> <plugins> <!-- Use the maven-assembly-plugin plugin to package --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>org.binbin.App</mainClass> </manifest> </archive> <descriptorRefs> <!-- End of executable jar name --> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <!--Configuration to generate Javadoc package--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.4</version> <configuration> <!--Specify encoding as UTF-8--> <encoding>UTF-8</encoding> <aggregate>true</aggregate> <charset>UTF-8</charset> <docencoding>UTF-8</docencoding> </configuration> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!--Configuration to generate source code package--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Operation renderings
1. Menu bar
2. About
3. Function page