Create GUI Form

This commit is contained in:
Aelita4 2023-01-15 00:57:46 +01:00
parent 8666ad8752
commit d53a1c820a
3 changed files with 505 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package pl.mikorosa.dziecoin;
import pl.mikorosa.dziecoin.gui.MainFrame;
import java.util.ArrayList;
import java.util.List;
@ -56,5 +58,8 @@ public class Main {
System.out.println("Wallet 1: " + blockchain.getAddressBalance(w1.getAddress()));
System.out.println("Wallet 2: " + blockchain.getAddressBalance(w2.getAddress()));
System.out.println("Wallet 3: " + blockchain.getAddressBalance(w3.getAddress()));
MainFrame mf = new MainFrame();
mf.setVisible(true);
}
}

View File

@ -0,0 +1,326 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="pl.mikorosa.dziecoin.gui.MainFrame">
<grid id="27dc6" binding="panel" default-binding="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<tabbedpane id="4ae2b" binding="menu">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="37163" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Wallet"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c973" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Wallet Address"/>
</properties>
</component>
<component id="17888" class="javax.swing.JLabel" binding="walletAddress">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
<component id="b0694" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Balance"/>
</properties>
</component>
<component id="e867" class="javax.swing.JLabel" binding="balance">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
<component id="ddc2" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Blockchain Height"/>
</properties>
</component>
<component id="5aaf0" class="javax.swing.JLabel" binding="height">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Label"/>
</properties>
</component>
</children>
</grid>
<grid id="7cb7c" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="NFTs"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="a05cf" class="javax.swing.JTree" binding="nftTree">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="8f6e" class="javax.swing.JButton" binding="createMintNFTButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Create (Mint) NFT"/>
</properties>
</component>
</children>
</grid>
<grid id="913bf" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Send"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="63e5b" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="fa750" class="javax.swing.JTextField" binding="amountToSend">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="bebf6" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Recipient"/>
</properties>
</component>
<component id="9abc4" class="javax.swing.JLabel" binding="sendAmount">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Amount"/>
</properties>
</component>
<component id="dd38" class="javax.swing.JTextField" binding="recipient">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<grid id="f9bc6" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="dbec0" class="javax.swing.JButton" binding="sendSubmit">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Send"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="11db8" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Mine Block"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="9377a" class="javax.swing.JTree" binding="mineBlockTree">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<grid id="a7d06" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="260f6" class="javax.swing.JButton" binding="mineBlockButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add transaction first"/>
</properties>
</component>
<component id="a7715" class="javax.swing.JButton" binding="clearButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Clear"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="5dd4a" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Blockchain"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="796fe" class="javax.swing.JTree" binding="blockchainTree">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="5289a" class="javax.swing.JButton" binding="validateChainButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Validate Chain"/>
</properties>
</component>
</children>
</grid>
<grid id="20f74" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="Export"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e13f5" class="javax.swing.JButton" binding="exportButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Export"/>
</properties>
</component>
<grid id="d179a" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5c684" class="javax.swing.JCheckBox" binding="exportBlockDataCheckBox" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Export block data"/>
</properties>
</component>
<component id="f77b2" class="javax.swing.JCheckBox" binding="exportTransactionDataCheckBox" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Export transaction data"/>
</properties>
</component>
<component id="2b8ee" class="javax.swing.JCheckBox" binding="exportNFTDataCheckBox" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Export NFT data"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="3e1bd" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<tabbedpane title="DzieCoinPay"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="b3f3b" class="javax.swing.JButton" binding="payForTuitionButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Pay for tuition"/>
</properties>
</component>
<component id="5789a" class="javax.swing.JButton" binding="payForAdvanceButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Pay for advance"/>
</properties>
</component>
</children>
</grid>
</children>
</tabbedpane>
</children>
</grid>
</form>

View File

@ -0,0 +1,174 @@
package pl.mikorosa.dziecoin.gui;
import pl.mikorosa.dziecoin.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;
import java.util.List;
public class MainFrame extends JFrame {
Blockchain bc;
String addr;
public JPanel panel;
private JTabbedPane menu;
private JLabel walletAddress;
private JLabel balance;
private JLabel height;
private JTree blockchainTree;
private JTree nftTree;
private JButton createMintNFTButton;
private JTextField amountToSend;
private JButton sendSubmit;
private JComboBox sendRecipient;
private JLabel sendAmount;
private JTree mineBlockTree;
private JButton mineBlockButton;
private JButton payForTuitionButton;
private JButton payForAdvanceButton;
private JButton validateChainButton;
private JTextField recipient;
private JButton exportButton;
private JCheckBox exportBlockDataCheckBox;
private JCheckBox exportTransactionDataCheckBox;
private JCheckBox exportNFTDataCheckBox;
private JButton clearButton;
private DefaultMutableTreeNode rootBlocks;
private DefaultMutableTreeNode rootNFT;
private DefaultMutableTreeNode rootMineBlock;
private List<Transaction> transactions;
private List<NFT> nfts;
private int howMuchLeftToSpend;
public MainFrame() {
super("DzieCoin");
transactions = new ArrayList<>();
nfts = new ArrayList<>();
mineBlockButton.setEnabled(false);
clearButton.setEnabled(false);
this.bc = Main.blockchain;
addr = this.bc.getBlocks().get(0).getTransactions().get(0).getRecipient();
howMuchLeftToSpend = this.bc.getAddressBalance(addr);
this.walletAddress.setText(addr);
this.setContentPane(this.panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(640, 360));
this.pack();
this.balance.setText(Integer.toString(this.bc.getAddressBalance(addr)));
this.height.setText(Integer.toString(this.bc.getBlocks().size()));
JTreeBlocks();
JTreeNFTs();
rootMineBlock = new DefaultMutableTreeNode("Objects to add to block");
rootMineBlock.removeAllChildren();
DefaultTreeModel model = new DefaultTreeModel(rootMineBlock);
mineBlockTree.setModel(model);
sendSubmit.addActionListener(e -> {
});
validateChainButton.addActionListener(e -> {
});
payForTuitionButton.addActionListener(e -> {
});
payForAdvanceButton.addActionListener(e -> {
});
mineBlockButton.addActionListener(e -> {
});
clearButton.addActionListener(e -> {
});
exportButton.addActionListener(e -> {
});
createMintNFTButton.addActionListener(e -> {
});
}
private void JTreeBlocks() {
rootBlocks = new DefaultMutableTreeNode("Blockchain");
for (Block block : this.bc.getBlocks()) {
DefaultMutableTreeNode b = new DefaultMutableTreeNode(block.getHeight() + " (" + block.getHash() + ")");
rootBlocks.add(b);
int i = 1;
for (Transaction transaction : block.getTransactions()) {
DefaultMutableTreeNode t = new DefaultMutableTreeNode("Transaction " + i++);
t.add(new DefaultMutableTreeNode("Sender: " + transaction.getSender()));
t.add(new DefaultMutableTreeNode("Recipient: " + transaction.getRecipient()));
t.add(new DefaultMutableTreeNode("Amount: " + transaction.getAmount()));
b.add(t);
}
};
DefaultTreeModel model = new DefaultTreeModel(rootBlocks);
blockchainTree.setModel(model);
}
private void JTreeNFTs() {
rootNFT = new DefaultMutableTreeNode("NFTs");
for (Block block : this.bc.getBlocks()) {
for (NFT nft : block.getNFTs()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(nft.getData());
n.add(new DefaultMutableTreeNode("Contract: " + nft.getContract()));
n.add(new DefaultMutableTreeNode("Owner: " + nft.getOwner()));
n.add(new DefaultMutableTreeNode("In block: " + block.getHeight()));
rootNFT.add(n);
}
};
DefaultTreeModel model = new DefaultTreeModel(rootNFT);
nftTree.setModel(model);
}
public void onNewBlockUpdate() {
Block newestBlock = this.bc.getLatestBlock();
this.balance.setText(Integer.toString(this.bc.getAddressBalance(addr)));
this.height.setText(Integer.toString(this.bc.getBlocks().size()));
DefaultMutableTreeNode b = new DefaultMutableTreeNode(newestBlock.getHeight() + " (" + newestBlock.getHash() + ")");
rootBlocks.add(b);
int i = 1;
for (Transaction transaction : newestBlock.getTransactions()) {
DefaultMutableTreeNode t = new DefaultMutableTreeNode("Transaction " + i++);
t.add(new DefaultMutableTreeNode("Sender: " + transaction.getSender()));
t.add(new DefaultMutableTreeNode("Recipient: " + transaction.getRecipient()));
t.add(new DefaultMutableTreeNode("Amount: " + transaction.getAmount()));
b.add(t);
}
DefaultTreeModel model = new DefaultTreeModel(rootBlocks);
blockchainTree.setModel(model);
for (NFT nft : newestBlock.getNFTs()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(nft.getData());
n.add(new DefaultMutableTreeNode("Contract: " + nft.getContract()));
n.add(new DefaultMutableTreeNode("Owner: " + nft.getOwner()));
n.add(new DefaultMutableTreeNode("In block: " + newestBlock.getHeight()));
rootNFT.add(n);
}
DefaultTreeModel model1 = new DefaultTreeModel(rootNFT);
nftTree.setModel(model1);
}
}