Java课程设计之银行管理系统(实现GUI可视化+IO流储存数据)

  • Post author:
  • Post category:java


本人学校在大二时有Java课程设计,本打算在网上找一个简单的银行管理系统借鉴一下,后来得知老师要求要实现可视化,在网上多次查找没有理想的可视化银行管理系统,无奈花费一星期自学了GUI方面的代码,课上老师也没有教过。经过一个多星期的奋战,解决了很多问题,最终完成此系统。闲来无事就想把代码分享出来,供学弟学妹们借鉴,如果觉得有帮助,希望可以点赞,如果经济富裕可以打赏,感激不尽。

(图片为部分运行结果)

代码初期想分为多个类,后来发现跳转界面时会出现很多问题,就把所有可视化代码放在一个类中,数据类单独写出来,应该还是比较精简易懂。其中可视化部分纯手工打造,数据类有所借鉴。运行代码之前需要在eclipse上下载swing插件。

此银行管理系统分为人工服务和ATM服务,功能划分可能不太合理,不过功能应该是很齐全。人工服务可以创建银行卡和登录银行卡。ATM服务可以登录和修改密码。具体功能包括取钱、存钱、查询操作记录。查询操作记录是根据本机时间生成记录。创建银行卡会生成txt文件,信息保存在txt文件中,如果正常使用肯定是不安全的,等以后再完善。

首先是用户信息类

Account.java

package data;
//import com.sun.deploy.util.SyncFileAccess;
import com.sun.org.apache.regexp.internal.RE;


import GUI.demo1;

import javax.swing.*;
import  java.io.*;
import java.text.SimpleDateFormat;
import  java.util.*;

public class Account {
	public int money;
    public String id;//账号名
    public String Name;//姓名
    public String PhoneNumber;//手机号
    public String IdCard;//身份证
    public String password;
    Date now=new Date();
    Date currentTime;
    SimpleDateFormat formatter;
    Reader fr;
    ;
    public Account(String id, String password, String money) {//构造方法
        this.id = id;
        this.password = password;
        this.money=Integer.parseInt(money);
    }
 
 
 
 
 
 
 
    public void outMoney (int money)throws Exception {//抛出异常,由相关的界面类弹窗处理异常,下面几个方法同理
        //如在取钱界面取钱,则会调用此函数,进行try/catch处理,获得这个函数的异常,弹窗说明异常
        if (money > this.money) {
            throw new Exception("余额不足");
        }
        if(money<0)
        {
            throw new Exception("不能取出负数");
        }
        formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//时间格式
        currentTime = new Date();//当前时间
        String dateString = formatter.format(currentTime);//处理当前时间格式
        Writer fw = new FileWriter(demo1.file);
        fw.write(demo1.recordString.append(dateString + "\t" + demo1.currentAccount.id + "\t取出" + money + "元\r\n").toString());//将这次的取钱行为添加到记录文件中
        fw.flush();//写进文件
        fw.close();
        this.money -= money;
        demo1.usersListUpdate();//更新用户文档(信息)
    }
 
    public void inMoney(int money)throws Exception
    {
        try {
            Writer fw = new FileWriter(demo1.file);
           // System.out.println(Test.file);
            formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            currentTime=new Date();
            String dateString=formatter.format(currentTime);
            fw.write(demo1.recordString.append(dateString+"\t"+demo1.currentAccount.id+"\t存入" + money + "元\r\n").toString());
            fw.flush();//写进文件
            fw.close();
 
            this.money+=money;
 
            demo1.usersListUpdate();//更新当前用户信息
 
        }
        catch (Exception e1)
        {
            throw new Exception("写入记录失败");
        }
 
    }
 

 
    public void ChangePassword(String newPassword)throws Exception
    {
     if(newPassword.equals(this.password))
     {
         throw new Exception("原密码和新密码不能一样");
     }
 
     else
     {
         if(newPassword.equals(""))
         {
             throw new Exception("密码不能为空");
         }
 
     }
     password=newPassword;
        demo1.usersListUpdate();
 
 
    }

}

然后就是最主要的可视化部分代码,可能有些长,但是都有注释,各个功能模块都有分开写,如果有看不懂可以私聊我.

demo1.java

package GUI;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import data.Account;

public class demo1  {
	 public static List<Account> usersList;
	    public static Account currentAccount;//登录的用户
	    public static File file;//当前用户的记录文件
	    public static StringBuilder recordString=new StringBuilder();//登录后读取文本中的记录,然后和recordString拼接
	    public static File usersFile;
	    public static StringBuilder usersString=new StringBuilder();
	    Boolean regirsterable=true;//控制是否能注册的变量
	 
	     static Reader fw;

		
	public static void main(String[] args)throws Exception {
		
		
		usersList = new ArrayList<Account>();
		 
        /**********************用户文本**********************/
        File users = new File("users.txt");
 
        if (!users.exists()) {
            try {
                users.createNewFile();
                Writer fw = new FileWriter("users.txt");
                fw.write("6000  12345  8888");
                fw.flush();
                fw.close();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null, "创建用户文档失败");
            }
 
        }
        usersFile = users;//创建用户文档,存储用户账户,密码,余额信息;
        usersListRead();
        usersListUpdate();
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(() -> zhujiemian());
	}
	  public static void usersListRead()
	    {
	        /**********************按照用户文档读取用户列表并创建所有用户**********************/
	        /**********************并写入list**********************/
	        try {
	            fw = new FileReader("users.txt");//字符流
	        } catch (Exception e) {
	            System.out.println("字符流创建失败");
	        }
	 
	        BufferedReader bfr = new BufferedReader(fw);
	 
	        String temp = "";
	        try {
	 
	            System.out.println("开始写入list");
	            while ((temp = bfr.readLine()) != null) {//不知为何读取出的字符串中最前面会出现Null
	                String[] tmpstr = new String[5];
	                tmpstr = temp.split("\\s+");//分割空格
	 
	                System.out.println("余额:" + tmpstr[2]);
	 
	                Account a = new Account(tmpstr[0], tmpstr[1], tmpstr[2]);
	                usersList.add(a);
	                System.out.println("读取到"+a.id+",实例化用户" + a.id);
	 
	            }
	            bfr.close();
	            fw.close();
	            System.out.println("用户list:"+usersList);
	        } catch (Exception e) {
	            System.out.println("读取用户文档失败");
	        }
	    }
	  public static void usersListUpdate()
	    {
	 
	 
	 
	        /**********************按照list内容写入文本用户信息**********************/
	        try {
	        Writer fw = new FileWriter("users.txt");
	 
	        StringBuilder tmpstr = new StringBuilder();
	        for (int i = 0; i < usersList.size(); i++) {
	            tmpstr.append(usersList.get(i).id + "    " + usersList.get(i).password + "    " + usersList.get(i).money + "\r\n");
	 
	        }
	        fw.write(tmpstr.toString());
	        fw.flush();
	        fw.close();
	    }
	    catch (Exception e)
	    {
	        e.printStackTrace();
	        System.out.println("更新用户失败");
	    }
	    }
	  
	private static  void zhujiemian() {
		JFrame jf = new JFrame("银行管理系统2.0");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setBounds(0, 0, 2030, 1980);
		jf.setResizable(false);
		jf.setVisible(true);
		//流式布局
		jf.setLayout(new FlowLayout(FlowLayout.CENTER,2030,150));
		JLabel label1 = new JLabel("---欢迎进入梦奇银行---",JLabel.CENTER);
		label1.setFont(new Font("宋体",Font.BOLD,60));
		JButton btn1 = new JButton("人工服务");
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		
		JButton btn2 = new JButton("ATM 服务");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn3 = new JButton("退出银行");
		btn3.setFont(new Font("宋体",Font.PLAIN,40));
		jf.add(label1);
		//加入rg1的监听
		jf.add(btn1);
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jf.dispose();
				// TODO Auto-generated method stub
				rg1();	
					}
				});
				
		//加入atm1的监听
		jf.add(btn2);
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf.dispose();
				atm1();
			}
		});
		jf.add(btn3);
		btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jf.dispose();
			}});}
	//进入人工服务
	private static void rg1() {
		JFrame jf2 = new JFrame("银行管理系统2.0");
		jf2.setVisible(true);
		jf2.setBounds(0, 0, 2030, 1980);
		jf2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf2.setLayout(new FlowLayout(FlowLayout.CENTER,2030,150));
		JLabel label2 = new JLabel("---梦奇银行---",JLabel.CENTER);
		label2.setFont(new Font("宋体",Font.BOLD,50));
		JButton btn1 = new JButton("创建银行卡");
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Register();
				jf2.dispose();;
			}
		});
		
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn2 = new JButton("登录银行卡");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn3 = new JButton("返回");
		btn3.setFont(new Font("宋体",Font.PLAIN,40));
		
		jf2.add(label2);
		jf2.add(btn1);
		//加入验证登录
		jf2.add(btn2);
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf2.dispose();
				VerifyEqual();
			}
		});
		jf2.add(btn3);
		btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jf2.dispose();
				zhujiemian();
	}
	});
	}
	
	
	
	//创建银行卡
			private static void Register() {
				JFrame jf2 = new JFrame("银行管理系统2.0");
				jf2.setVisible(true);
				jf2.setBounds(0, 0, 2030, 1980);
				jf2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				jf2.setLayout(new FlowLayout(FlowLayout.CENTER,2030,150));
				JLabel label1 = new JLabel("---梦奇银行---",JLabel.CENTER);
				label1.setFont(new Font("宋体",Font.BOLD,50));
				
				JPanel p0=new JPanel();
				JPanel p1=new JPanel();
				JPanel p2=new JPanel();
				JPanel p3=new JPanel();
				JLabel label2 = new JLabel("\t卡  号");
				label2.setFont(new Font("宋体",Font.BOLD,30));
				JTextField text1 = new JTextField(30);
				text1.setFont(new Font("宋体",Font.BOLD,30));
				JLabel label3 = new JLabel("\t密  码");
				label3.setFont(new Font("宋体",Font.BOLD,30));
				
				JPasswordField text2 = new JPasswordField(30);
				text2.setFont(new Font("宋体",Font.BOLD,30));
				JButton btn1 = new JButton("注 册");
				btn1.setFont(new Font("宋体",Font.PLAIN,40));
				jf2.add(p0);
				p0.add(label1);
				jf2.add(p1);
				p1.add(label2);
				p1.add(text1);
				jf2.add(p2);
				p2.add(label3);
				p2.add(text2);
				jf2.add(p3);
				p3.add(btn1);
				
				btn1.addActionListener(new ActionListener() {
					
					@SuppressWarnings("deprecation")
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						
						Boolean regirsterable=true;//控制是否能注册的变量
						if(regirsterable==true) {
			                if (text1.getText().equals(""))//如果userName的文本是空
			                {
			                    JOptionPane.showMessageDialog(jf2, "用户名不能为空");//弹窗
			                    return;
			                }
					}
						for (int i = 0; i < usersList.size(); i++) {
							 
		                    if (usersList.get(i).id.equals(text1.getText())) {
		                        JOptionPane.showMessageDialog(jf2, "该用户已被注册");
		                        text1.setText("");//清空输入框
		                        text2.setText("");
		                        return;//如果同名,结束方法,不在运行下面的语句
		                    }}
						 if (text1.getText().equals("")) {
			                    JOptionPane.showMessageDialog(jf2, "密码不能为空,请重新输入");
			                    return;
			 
			                } else {
			                    if (text2.getText()!=null) {
			                        Account registeraccount = new Account(text1.getText(), text2.getText(), "0");//实例化此账号
			                        JOptionPane.showMessageDialog(jf2, "注册成功,请登录");
			                        demo1.usersList.add(registeraccount);//加入Test类的静态用户list
			                        demo1.usersListUpdate();//更新用户文档
			 
			                        jf2.dispose();
			                        rg1();
			                    } 			                 
			                }
					}});
				
			}
	//进入ATM机服务
	private static void atm1(){
		JFrame jf3 = new JFrame("银行管理系统2.0");
		jf3.setVisible(true);
		jf3.setBounds(0, 0, 2030, 1980);
		jf3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf3.setLayout(new FlowLayout(FlowLayout.CENTER,2030,150));
		JLabel label2 = new JLabel("---梦奇银行---",JLabel.CENTER);
		label2.setFont(new Font("宋体",Font.BOLD,50));
		JButton btn1 = new JButton("更改密码");
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		
		
		//更改密码监听器
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf3.dispose();
				ChangePassword();
			}
		});
		
		
		JButton btn2 = new JButton("登录系统");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn3 = new JButton("返回");
		btn3.setFont(new Font("宋体",Font.PLAIN,40));
		
		jf3.add(label2);
		jf3.add(btn1);
		//登录系统监听器
		jf3.add(btn2);
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf3.dispose();
				VerifyEqual();
			}
		});
		//返回上一级
		jf3.add(btn3);
		btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jf3.dispose();
				zhujiemian();
	}
	});
	}
	
	//更改密码
	private static void ChangePassword() {
		JFrame iframe=new JFrame("更改密码");
        iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        iframe.setLayout((new FlowLayout(FlowLayout.CENTER,2030,150)));
        JPanel ip2=new JPanel();
        JLabel label1 =new JLabel("原密码:");
        ip2.add(label1);
        label1.setFont(new Font("宋体",Font.PLAIN,40));
        JTextField oldPassword=new JTextField(20);
        oldPassword.setFont(new Font("宋体",Font.PLAIN,40));
        ip2.add(new JLabel("<html><br/><html>"));//换行
        ip2.add(oldPassword);
 
        JPanel ip0=new JPanel();
        JLabel label2 =new JLabel("新密码:");
        ip2.add(label2);
        label2.setFont(new Font("宋体",Font.PLAIN,40));
        ip0.add(label2);
        JTextField newPassword=new JTextField(20);
        newPassword.setFont(new Font("宋体",Font.PLAIN,40));
        ip0.add(new JLabel("<html><br/><html>"));//换行
        ip0.add(newPassword);
 
        JPanel ip4=new JPanel();
        JLabel label3 =new JLabel("再次输入新密码:");
        label3.setFont(new Font("宋体",Font.PLAIN,40));
        ip4.add(label3);
        JTextField checkPassword=new JTextField(20);
        checkPassword.setFont(new Font("宋体",Font.PLAIN,40));
        ip4.add(new JLabel("<html><br/><html>"));//换行
        ip4.add(checkPassword);
 
        JPanel  ip3=new JPanel();
        JButton confirm=new JButton("确定");
        confirm.setFont(new Font("宋体",Font.PLAIN,40));
        ip3.add(confirm);
        JButton cancel=new JButton("返回");
        cancel.setFont(new Font("宋体",Font.PLAIN,40));
        ip3.add(cancel);
 
        iframe.add(ip2);
        iframe.add(ip0);
        iframe.add(ip4);
        iframe.add(ip3);
        
        iframe.setVisible(true);
 
        iframe.setBounds(0,0,2030,1980);
        
        
        //确定修改密码
        confirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
		            if (currentAccount.password.equals(oldPassword.getText())) {
		                try {
		                    if(newPassword.getText().equals(checkPassword.getText())) {
		 
		                        currentAccount.ChangePassword(newPassword.getText());
		                        JOptionPane.showMessageDialog(null, "更改密码成功");
		                        return;
		                    }
		                    else
		                    {
		                        JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
		                    }
		                }
		             catch (Exception e1) {//捕获账户类中更改密码函数的异常并弹窗显示
		                    JOptionPane.showMessageDialog(null, e1.getMessage());
		                }
		            } else {
		 
		                JOptionPane.showMessageDialog(null, "原密码错误");
		            }
		        
			}
		});
        cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				iframe.dispose();
				atm1();
			}
		});
		
		
		
		
		
		
		
		
		
		
		
	}
	
	
	
	//验证登录界面

	private static void VerifyEqual() {
		JFrame jf5 = new JFrame("登录");
		jf5.setVisible(true);
		jf5.setBounds(0, 0, 2030, 1980);
		jf5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf5.setLayout(new FlowLayout(FlowLayout.CENTER,2030,150));
		JPanel p0=new JPanel();
		JPanel p1=new JPanel();
		JPanel p2=new JPanel();
		JPanel p3=new JPanel();
	
		jf5.add(p0);
		jf5.add(p1);
		jf5.add(p2);
		jf5.add(p3);
		p3.setLayout(new FlowLayout(FlowLayout.CENTER,500,0));
	
		
       
		JLabel label1 = new JLabel("登 录");
		label1.setFont(new Font("宋体",Font.BOLD,70));
		JLabel label2 = new JLabel("\t卡  号");
		label2.setFont(new Font("宋体",Font.BOLD,30));
		JTextField text1 = new JTextField(30);
		
		JLabel label3 = new JLabel("\t密  码");
		label3.setFont(new Font("宋体",Font.BOLD,30));
		
		JPasswordField text2 = new JPasswordField(30);
		 Button btn1 = new Button("登 录");
		btn1.setFont(new Font("宋体",Font.BOLD,50));
		Button btn2 = new Button("返 回");
		btn2.setFont(new Font("宋体",Font.BOLD,50));
		p0.add(label1);
		p1.add(label2);
		p1.add(text1);
		p2.add(label3);
		p2.add(text2);
		p3.add(btn1);
		p3.add(btn2);
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf5.dispose();
				atm1();
			}
		});

		
			btn1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					jf5.dispose();
					
					if(e.getActionCommand().equals("登 录")){
			            for (int i = 0; i < usersList.size(); i++) {
			 
			                if (usersList.get(i).id.equals(text1.getText())) {
			 
			                    if(text2.getText().equals(usersList.get(i).password))
			                    {
			                        JOptionPane.showMessageDialog(jf5, "登录成功");
			                        currentAccount=usersList.get(i);//将list中符合登陆输入的账户密码的类设为当前Test类中的静态的“当前类”,以便后面各种操作;
			                        file=new File(currentAccount+".txt");
			                        recordString=new StringBuilder();//清空,避免将上一个用户的记录写进新登录的用户中
			                        //Test.recordString.append("");//避免recordString空指针
			                        menu();//实例化菜单窗口
			 
			                        
			                       jf5.dispose();//隐藏登陆窗口
			 
			                        /************************创建记录文件**********************/
			                        File records = new File(currentAccount.id+".txt");//以账户id命名
			                        if(!records.exists())
			                        {
			                            try {
			                                records.createNewFile();
			                            }
			                            catch (Exception e1)
			                            {
			                                JOptionPane.showMessageDialog(null, "创建该用户的记录失败");
			                            }
			                        }
			                       file=records;
			                        /*****************读取记录文件************/
			                        try {
			                             fw = new FileReader(file);//字符流
			                        }
			                        catch (Exception e1)
			                        {
			                            JOptionPane.showMessageDialog(null, "读取记录失败");
			                        }
			                        BufferedReader bfr=new BufferedReader(fw);
			 
			                        String temp="";
			                        try {
			                            while ((temp = bfr.readLine()) != null) {//不知为何读取出的字符串中最前面会出现Null,但不影响使用
			                                recordString .append(temp);//读取原先该账户的记录的每一行并拼接到recordString中,输出作为查询记录的结果
			                            }
			                            //将记录读取并合并为一个字符串
			                            fw.close();
			                        }
			                        catch (Exception e1)
			                        {
			                            System.out.println("读取记录过程中出现错误");
			                        } return;
			                    }
			                    else
			                    {
			                        JOptionPane.showMessageDialog(jf5, "密码错误");
			                        text2.setText("");
			                        VerifyEqual();
			                    }
			                }
			            }
			            JOptionPane.showMessageDialog(jf5, "该用户不存在");
			  }}});}
	
	//功能菜单界面
	private static void menu() {
		JFrame jf4 = new JFrame("银行管理系统2.0");
		jf4.setVisible(true);
		jf4.setBounds(0, 0, 2030, 1980);
		jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf4.setLayout(new FlowLayout(FlowLayout.CENTER,2030,120));
		JLabel label2 = new JLabel("---梦奇银行---",JLabel.CENTER);
		label2.setFont(new Font("宋体",Font.BOLD,50));
		JButton btn1 = new JButton("存钱");
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn2 = new JButton("取钱");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn3 = new JButton("查询");
		btn3.setFont(new Font("宋体",Font.PLAIN,40));
		
		JButton btn5 = new JButton("返回");
		btn5.setFont(new Font("宋体",Font.PLAIN,40));
		jf4.add(label2);
		jf4.add(btn1);
		jf4.add(btn2);
		jf4.add(btn3);
		
		//添加存钱监听器
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				SaveMoney();
			}
		});
		//添加取钱监听器
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				OUTMoney();
			}
		});
		//添加查询监听器
		btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				Inqury();
			}
		});
		//返回上一级
		jf4.add(btn5);
btn5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jf4.dispose();
				atm1();
	}
	});
	}
	
//存钱
	private static void SaveMoney() {
		
		JFrame jf4 = new JFrame("银行管理系统2.0");
		jf4.setVisible(true);
		jf4.setBounds(0, 0, 2030, 1980);
		jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf4.setLayout(new FlowLayout(FlowLayout.CENTER,2030,130));
		JPanel ip0=new JPanel();
		JPanel ip1=new JPanel();
		JPanel ip11=new JPanel();
		JPanel ip2=new JPanel();
		JPanel ip3=new JPanel();

		JLabel label2 = new JLabel("---梦奇银行---",JLabel.CENTER);
		label2.setFont(new Font("宋体",Font.BOLD,50));
		ip0.add(label2);
		JLabel label1 = new JLabel("账户余额:"+currentAccount.money,JLabel.CENTER);
		label1.setFont(new Font("宋体",Font.BOLD,40));
		ip11.add(label1);
		JLabel label3 = new JLabel("***请输入存款金额***",JLabel.CENTER);
		label3.setFont(new Font("宋体",Font.BOLD,40));
		ip1.add(label3);
		JTextField text = new JTextField(100);
		text.setFont(new Font("宋体",Font.BOLD,30));
		ip2.add(text);
		ip3.setLayout(new FlowLayout(FlowLayout.CENTER,500,0));
		JButton btn1 = new JButton("确 定");
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn2 = new JButton("返 回");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		ip3.add(btn1);
		ip3.add(btn2);
		jf4.add(ip0);
		jf4.add(ip1);
		jf4.add(ip11);
		jf4.add(ip2);
		jf4.add(ip3);
		
		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			 try {   
					currentAccount.inMoney(Integer.parseInt(text.getText()));//调用当前登陆账户的存钱函数
		 
		                JOptionPane.showMessageDialog(null, "存款成功");//弹窗
		                label1.setText("账户余额:"+currentAccount.money);
		                
		            }
		            catch (ClassCastException e1)//捕获当前登录账户中函数中的异常。类型转换异常
		            {
		                JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");
		            }
		            catch (Exception e1)//
		            {
		                JOptionPane.showMessageDialog(null, e1.getMessage());
		            }}
			});
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				menu();
			}
		});
	}
	//取钱
	private static void OUTMoney() {
		JFrame jf4 = new JFrame("银行管理系统2.0");
		jf4.setVisible(true);
		jf4.setBounds(0, 0, 2030, 1980);
		jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf4.setLayout(new FlowLayout(FlowLayout.CENTER,2030,100));
		JPanel ip0=new JPanel();
		JPanel ip1=new JPanel();
		JPanel ip11=new JPanel();
		JPanel ip111=new JPanel();
		JPanel ip1111=new JPanel();
		JPanel ip2=new JPanel();
		JPanel ip3=new JPanel();
		JLabel label1 = new JLabel("---梦奇银行---",JLabel.CENTER);
		label1.setFont(new Font("宋体",Font.BOLD,50));
		ip0.add(label1);
		JLabel label2 = new JLabel("卡号:"+currentAccount.id,JLabel.CENTER);
		label2.setFont(new Font("宋体",Font.BOLD,30));
		JLabel label3 = new JLabel("账户余额:"+currentAccount.money,JLabel.CENTER);
		label3.setFont(new Font("宋体",Font.BOLD,30));
		ip1.add(label2);
		ip11.add(label2);
		ip111.add(label3);
		JLabel label4 = new JLabel("***请输入取款金额***",JLabel.CENTER);
		label4.setFont(new Font("宋体",Font.BOLD,30));
		ip1111.add(label4);
		JTextField text = new JTextField(100);
		text.setFont(new Font("宋体",Font.BOLD,30));
		ip2.add(text);
		ip3.setLayout(new FlowLayout(FlowLayout.CENTER,500,0));
		JButton btn1 = new JButton("确 定");
		btn1.setFont(new Font("宋体",Font.PLAIN,40));
		JButton btn2 = new JButton("返 回");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		ip3.add(btn1);
		ip3.add(btn2);
		jf4.add(ip0);
		jf4.add(ip1);
		jf4.add(ip11);
		jf4.add(ip111);
		jf4.add(ip2);
		jf4.add(ip3);
		//添加取钱监听器
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					 
	                currentAccount.outMoney(Integer.parseInt(text.getText()));
	 
	                JOptionPane.showMessageDialog(null, "取款成功");//弹窗
	                label3.setText("账户余额:"+currentAccount.money);//更新余额显示
	            }
	            catch (ClassCastException e1)
	            {
	                JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");//捕获Test类中outmoney方法的异常
	            }
	            catch (Exception e1)
	            {
	                JOptionPane.showMessageDialog(null, e1.getMessage());
	            }
			}
		});
		//返回菜单
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				menu();
			}
		});
	}
	//查询功能
	private static void Inqury() {
		JFrame jf4 = new JFrame("梦奇银行查询");
		jf4.setVisible(true);
		jf4.setBounds(0, 0, 2030, 1980);
		jf4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf4.setLayout(new FlowLayout(FlowLayout.CENTER,2030,130));
		JPanel ip0=new JPanel();
		JLabel label1 = new JLabel("卡号:"+currentAccount.id);
		label1.setFont(new Font("宋体",Font.PLAIN,40));
        ip0.add(label1);
        JPanel ip1=new JPanel();
        JLabel yue=new JLabel("账户余额:"+currentAccount.money);
        yue.setFont(new Font("宋体",Font.PLAIN,40));
        ip1.add(yue);
        JPanel ip2=new JPanel();
        JPanel ip3=new JPanel();
        JTextArea inquryresult=new JTextArea(10,40);
        ip2.add(inquryresult);
        JButton confirm=new JButton("查询记录");
        confirm.setFont(new Font("宋体",Font.PLAIN,40));
        JButton btn2 = new JButton("返 回");
		btn2.setFont(new Font("宋体",Font.PLAIN,40));
		ip3.setLayout(new FlowLayout(FlowLayout.CENTER,500,0));
		ip3.add(confirm);
		ip3.add(btn2);
        jf4.add(ip0);
        jf4.add(ip1);
        jf4.add(ip2);
        jf4.add(ip3);
        confirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				inquryresult.setText(recordString.toString().replace("元","元\n").replace("null",""));//去除掉结果字符串中的null,并将元替换为元\r\n来换行换行
	            yue.setText("账户余额:"+currentAccount.money);//更新显示余额
			}
		});
      
        //返回上一级
        btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jf4.dispose();
				menu();
			}
		});
		
	}
}



版权声明:本文为m0_58167559原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。