利用线程,完成银行存取款的简单操作。
BankUI.java
package cn.antraces.GUI;
import cn.antraces.GUI.Thread.Bank;
import cn.antraces.GUI.Thread.BankThread;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class BankUI {
public JFrame jf;
public JPanel jp;
public JTextField jTextField1, jTextField2;
public JPasswordField jPasswordField;
public JLabel jLabel1, jLabel2, jLabel3, jLabel4, jLabel5;
public JButton post, reset;
private Bank bank = new Bank();
public BankUI(){
jf = new JFrame("香草银行");
jf.setSize(400, 200);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
jp = new JPanel(null);
jLabel1 = new JLabel();
jLabel1.setText("账号: ");
jLabel1.setFont(new Font(null, Font.PLAIN, 18));
jLabel1.setBounds(75, 5, 100,20);
jLabel2 =new JLabel();
jLabel2.setText("密码: ");
jLabel2.setFont(new Font(null, Font.PLAIN, 18));
jLabel2.setBounds(75, 30, 100,20);
jLabel3 = new JLabel();
jLabel3.setText("请输入密码");
jLabel3.setFont(new Font(null, Font.PLAIN, 18));
jLabel3.setBounds(120, 50, 160, 40 );
jLabel4 = new JLabel();
jLabel4.setText("余额: 0 香草");
jLabel4.setFont(new Font(null, Font.PLAIN, 18));
jLabel4.setBounds(60, 80, 290, 40 );
jLabel4.setVisible(false);
jLabel5 = new JLabel();
jLabel5.setText("");
jLabel5.setFont(new Font(null, Font.PLAIN, 18));
jLabel5.setBounds(60, 110, 290, 40 );
jLabel5.setVisible(false);
jTextField1 = new JTextField(16);
jTextField1.setFont(new Font(null, Font.PLAIN, 18));
jTextField1.setBounds(125, 5, 180, 20);
jTextField2 = new JTextField(16);
jTextField2.setFont(new Font(null, Font.PLAIN, 18));
jTextField2.setBounds(125, 30, 180, 20);
jTextField2.setVisible(false);
jTextField2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) { //设置只允许整型数字
char keyChar = e.getKeyChar();
if(!(keyChar >= '0' && keyChar <= '9')){
e.consume();
}
}
});
jPasswordField = new JPasswordField(16);
jPasswordField.setFont(new Font(null, Font.PLAIN, 18));
jPasswordField.setBounds(125, 30, 180, 20);
post = new JButton("登陆");
post.setBounds(60, 60, 60, 25);
post.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getActionCommand().equals("登陆")) {
String account = new String(jTextField1.getText());
String psw = new String(jPasswordField.getPassword());
if (account.equals("silver") && psw.equals("moon")) {
jLabel3.setText("已登陆(。・ω・。)ノ♡");
jPasswordField.setVisible(false);
jTextField2.setVisible(true);
jTextField1.setEditable(false);
jLabel2.setText("数量: ");
post.setText("存款");
reset.setText("取款");
jLabel4.setVisible(true);
jLabel5.setVisible(true);
} else {
jLabel3.setText("账号或密码错误");
}
} else if (actionEvent.getActionCommand().equals("存款")) {
Thread thread1 = new BankThread(bank, Integer.parseInt(jTextField2.getText()),
1, jLabel5, jLabel4);
thread1.start();
}
}
});
reset = new JButton("重置");
reset.setBounds(260, 60, 60, 25);
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getActionCommand().equals("重置")) {
jTextField1.setText("");
jPasswordField.setText("");
} else if (actionEvent.getActionCommand().equals("取款")) {
Thread thread1 = new BankThread(bank, Integer.parseInt(jTextField2.getText()),
-1, jLabel5, jLabel4);
thread1.start();
}
}
});
jp.add(jLabel1);
jp.add(jTextField1);
jp.add(jTextField2);
jp.add(jLabel2);
jp.add(jPasswordField);
jp.add(post);
jp.add(reset);
jp.add(jLabel3);
jp.add(jLabel4);
jp.add(jLabel5);
jf.setContentPane(jp);
jf.setVisible(true);
}
public static void main (String[] args){
new BankUI();
}
`}
`
Thread/Bank.java
package cn.antraces.GUI.Thread;
public class Bank {
private int money;
public String deposit(int num){
this.money = this.money + num;
return "成功存入 " + num + " 香草";
}
public String withdraw(int num){
if(this.money > 0 && num > 0 && this.money - num >= 0){
this.money = this.money - num;
return "成功取出 " + num + " 香草";
}else{
return "余额不足 " + num + " 香草";
}
}
public int balance(){
return this.money;
}
`}
`
Thread/BankThread.java
package cn.antraces.GUI.Thread;
import javax.swing.\*;
public class BankThread extends Thread {
private Bank myBank;
private int num, mode;
private JLabel jLabel1, jLabel2;
public BankThread(Bank bank, int num, int mode, JLabel jLabel1, JLabel jLabel2) {
this.myBank = bank;
this.num = num;
this.mode = mode;
this.jLabel1 = jLabel1;
this.jLabel2 = jLabel2;
}
@Override
public void run() {
synchronized (myBank){
if (mode == 1) {
jLabel1.setText(myBank.deposit(this.num));
} else if (mode == -1) {
jLabel1.setText(myBank.withdraw(this.num));
}
jLabel2.setText("余额: " + myBank.balance() + " 香草");
}
try {
this.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
`}
`
早知道就不写这UI了, 麻烦还丑