前几天心血来潮,用Java模拟实现了一个区块链小工具,该工具是单机版的,没有实现联网功能,属于是单节点的工具,但是对于区块链的核心wk逻辑功能都实现了,如:wk、钱包、转账、记账等功能。界面实现用到了Java的swing包。
下面先看下运行的效果:
主界面:
转账:
下面来看看代码:
首先是界面:
package com.yuanlrc.app;import java.awt.Color;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.List;import java.util.UUID;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.border.EmptyBorder;import javax.swing.border.TitledBorder;import com.yuanlrc.model.Block;import com.yuanlrc.model.Transaction;import com.yuanlrc.thread.MinBlockThread;import com.yuanlrc.util.CryptoUtil;public class App extends JFrame {/** **/private static final long serialVersionUID = 1L;private JPanel contentPane;private JTextField recieverAddressTextField;private JTextArea showRstTextArea;private String address;//当前钱包地址private List blockChain;//区块链private MinBlockThread minBlockThread;private List txs = new ArrayList();public JProgressBar currentStatusProgressBar;private JTextField amountTextField;/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {App frame = new App();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public App() {try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (UnsupportedLookAndFeelException e) {// TODO Auto-generated catch blocke.printStackTrace();}address = UUID.randomUUID().toString();setTitle("【\u733F\u5E01\u6316\u77FF\u5DE5\u5177】钱包地址:" + address +"【当前余额:0】" );setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 717, 571);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);JPanel panel = new JPanel();panel.setBorder(new TitledBorder(null, "\u524D\u7F6E\u52A8\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));JPanel panel_1 = new JPanel();panel_1.setBorder(new TitledBorder(null, "\u8F6C\u8D26\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));JPanel panel_2 = new JPanel();panel_2.setBorder(new TitledBorder(null, "\u6316\u77FF\u7ED3\u679C", TitledBorder.LEADING, TitledBorder.TOP, null, null));JPanel panel_3 = new JPanel();panel_3.setBorder(new TitledBorder(null, "\u5F53\u524D\u72B6\u6001", TitledBorder.LEADING, TitledBorder.TOP, null, null));GroupLayout gl_contentPane = new GroupLayout(contentPane);gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup().addGap(18).addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false).addComponent(panel_2, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(panel_3, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(panel_1, GroupLayout.DEFAULT_SIZE, G