[课程设计]Java实现图形化窗口界面可存储ATM机(自助取款机)

[很久以前写的了,没想到这么多浏览了,现在看着不是一般的乱..当时刚学java,望见谅,不过现在也没有闲工夫重写和心情修改了,凑活凑活吧]

这是一个使用io流和swing库制作的可存储的窗口化ATM机程序;臭不要脸的发上来敲打

实现的功能有:1.登录和注册用户(虽然现实中的ATM中没有注册功能敲打

2.存款

3.取款

4.查询记录,包括存款和取款和转账的记录

5.更改密码

6.退卡


类的构成:1.Test类,实现读取用户文档并更新用户文档的功能;

2.LoginGui类,登录界面,实现登录和注册等功能;

3.Menu类,菜单界面

4.InMoney,OutMoney,Inqury,Transfer类,ChangePassword,分别为存款界面,取款界面,查询界面,转账界面,更改密码界面

5.Account,账户类,实现账户的各种功能,包括存款,取款,转账,更改密码等


项目思路:1.该项目中通过文本来保存用户信息和操作记录,用户信息存储在users.txt中,用户的记录存储在相应用户的用户名.txt中,在程序开始时,调用Test类中的

usesrListRead方法读取users文档,将信息读入程序,得到用户信息,创建用户的List(Tset类中的usersList),并创建用户;


2.首先在登陆界面进行登陆或注册,进行合法性验证,若能成功注册,创建相应的Account类,并创建相应的记录文本,然后要求登录。如果能成功登陆,将Tes

t中的静态变量currentAccount(当前登录的用户)设置为该用户,之后的操作对这个用户进行操作。本程序中

有一个默认用户,id为admin,密码为123456,可以直接登录。记录文本用来存储用户的操作,在后面的每一步操

作中都会通过io流写入记录文本;


3.登陆后弹出菜单,点击按钮弹出相应界面,使用功能后将记录根据文件名存储在用户相应的记录文件中,然后每一次用户状态改变时(使用功能后)都会调

用Test类中的usersListUpdate对用户文档进行更新。这样关闭程序后下次再打开程序时再读取文档就能恢复信息。也就是完成了保存功能。

4.各个界面均是使用java自带的swing库实现的。


注意:1.Test类中使用了很多静态变量来进行全局传值。Test.xxx什么都表示是Test  类中的静态变量。

     2.不能同时用read和write对同一个文件进行操作,否则会清空文件 ,应注意

流的关闭


源码下载地址:链接:点击打开链接 点击打开链接 密码: 3fy5


实现的功能截图:

登录:


菜单及总功能界面:




具体代码:

Test类(测试类)

package mybank;

import javafx.scene.layout.Pane;

import javax.swing.*;

import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static List<Account> usersList;
    public static Account currentAccount;//登录的用户
    public static File file;//当前用户的记录文件
    public static StringBuilder recordString=new StringBuilder();//登录后读取文本中的记录,然后和recordString拼接
    public static Menu menu;//静态的菜单界面,用于在更换密码后关闭菜单界面
    public static File usersFile;
    public static StringBuilder usersString=new StringBuilder();


     static Reader fw;

    public static void main(String[] args)throws Exception {

        usersList = new ArrayList<Account>();

        //System.out.println(usersList);
        /**********************用户文本**********************/
        File users = new File("users.txt");

        if (!users.exists()) {
            try {
                users.createNewFile();
                Writer fw = new FileWriter("users.txt");
                fw.write("admin  12345   88888");
                fw.flush();
                fw.close();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null, "创建用户文档失败");
            }

        }
        usersFile = users;//创建用户文档,存储用户账户,密码,余额信息;
        usersListRead();
        usersListUpdate();
        /*****************************Login****************************/

        LoginGui loginGui = new LoginGui();
    }
    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++) {
           // System.out.println(Test.currentAccount.id);
            tmpstr.append(usersList.get(i).id + "    " + usersList.get(i).password + "    " + usersList.get(i).money + "\r\n");

            //fw.write(Test.currentAccount.id + "    " + Test.currentAccount.password + "    " + Test.currentAccount.money+"\r\n");
        }
        fw.write(tmpstr.toString());
        fw.flush();
        fw.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("更新用户失败");
    }

    }
}

Account类(账户类)

方法主要写在这里面。

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

import javax.swing.*;
import  java.io.*;
import java.text.SimpleDateFormat;
import  java.util.*;
public class Account {
    int money;
    String id;//账号名

    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(Test.file);
        fw.write(Test.recordString.append(dateString + "\t" + Test.currentAccount.id + "\t取出" + money + "元\r\n").toString());//将这次的取钱行为添加到记录文件中
        fw.flush();//写进文件
        fw.close();
        this.money -= money;
        Test.usersListUpdate();//更新用户文档(信息)
    }

    public void inMoney(int money)throws Exception
    {
        try {
            Writer fw = new FileWriter(Test.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(Test.recordString.append(dateString+"\t"+Test.currentAccount.id+"\t存入" + money + "元\r\n").toString());
            fw.flush();//写进文件
            fw.close();

            this.money+=money;

            Test.usersListUpdate();//更新当前用户信息

        }
        catch (Exception e1)
        {
            throw new Exception("写入记录失败");
        }

    }

    public void transfer(int money,String id)throws Exception//转账
    {
        if(id.equals(Test.currentAccount.id))
        {
            throw new Exception("不能转给自己");
        }
        if(money>this.money)
        {
            throw new Exception("余额不足");
        }
        if(money<0) {
            throw new Exception("不能转入负数");
        }


        for(int i=0;i<Test.usersList.size();i++)
        {
            if(Test.usersList.get(i).id.equals(id))//找到要转帐的用户
            {
                Test.usersList.get(i).money+=money;//转入
                this.money-=money;//扣钱

                FileWriter fw=new FileWriter(Test.file);
                formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//声明时间格式
                currentTime=new Date();//获取当前时间
                String dateString=formatter.format(currentTime);//转换时间格式
                fw.write(Test.recordString.append(dateString+"\t向"+id+"\t转出"+money+"元\r\n").toString());//Test类中的静态字符串拼接上这个字符串覆盖写入当前用户文档
                fw.close();

                /********************向转入目标写入转账信息*************************/
                try {
                fr = new FileReader(id+".txt");//字符流
                 }
                 catch (Exception e)
                 {
                System.out.println("字符流创建失败");
                }

                BufferedReader bfr = new BufferedReader(fr);

                String temp="";
                String temp1;

                while ((temp1 = bfr.readLine()) != null)
                {
                    temp+=temp1;
                }
                temp=temp.replace("元","元\n\r")+dateString+"\t由"+Test.currentAccount.id+"\t转进"+money+"元\r\n";
                System.out.println(temp);
                fw=new FileWriter(id+".txt");
                fw.write(temp);
                fw.close();


                JOptionPane.showMessageDialog(null,"转账成功");
                Test.usersListUpdate();//更新用户文档

                return;
            }
        }
        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;
        Test.usersListUpdate();


    }



}


LoginGui类(登录界面)

package mybank;

import javax.swing.*;
import java.awt.*;
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.Reader;


public class LoginGui implements ActionListener{//实现监听器的接口
    private JFrame frame;
    private JPanel p0,p1,p2,p3,p4;//p4包括确认密码时的输入框;点击register按钮出现

    private JTextField userName;
    private JTextField passWord,passwordCheck;
    private JButton login;
    private JButton register;
    private Reader fw;
    Boolean regirsterable=true;//控制是否能注册的变量


    public LoginGui() {
        frame=new JFrame("登录ATM");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗口的点击右上角的x的处理方式,这里设置的是退出程序
        p0=new JPanel();

        p0.add(new JLabel("中国邮政储蓄银行ATM"));
        frame.add(p0);

        p1=new JPanel();
        p1.add(new JLabel("\tUserName:"));
        userName=new JTextField(20);
        p1.add(userName);

        p2=new JPanel();
        p2.add(new JLabel("\tPassword:"));
        passWord=new JTextField(20);
        p2.add(passWord);


        p3=new JPanel();

        login=new JButton("     Login    ");
        register=new JButton("   Register   ");
        p3.add(login);
        p3.add(register);

        p4=new JPanel();
        p4.add(new JLabel("PasswordCheck:"));
        passwordCheck=new JTextField(20);
        p4.add(passwordCheck);


        frame.add(p1);
        frame.add(p2);
        frame.add(p4);//确认密码框
        frame.add(p3);


        frame.pack();
        frame.setVisible(true);
        p4.setVisible(false);
        show();
        /*****************************Login****************************/
    }



    public void show(){

        login.addActionListener(this);//添加监视器
        register.addActionListener(this);
        frame.setBounds(500,500,350,250);//设置大小
        frame.setLayout(new FlowLayout());//设置流式布局
    }




    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand().equals("   Register   ")) {//注册,如果监听器获得的按钮文本是这个,也就是点击的按钮文本是这个的话,执行下面的语句
            if(p4.isVisible()==false)
            {
                p4.setVisible(true);//检查密码输入栏
                login.setText("     Cancel    ");//将login文本改为cancel,同时也能触发作为Cancel的监听器
                regirsterable=true;
                return;
            }
            if(regirsterable==true) {
                if (userName.getText().equals(""))//如果userName的文本是空
                {
                    JOptionPane.showMessageDialog(frame, "用户名不能为空");//弹窗
                    return;
                }


                for (int i = 0; i < Test.usersList.size(); i++) {

                    if (Test.usersList.get(i).id.equals(userName.getText())) {
                        JOptionPane.showMessageDialog(frame, "该用户已被注册");
                        userName.setText("");//清空输入框
                        passWord.setText("");
                        passwordCheck.setText("");
                        return;//如果同名,结束方法,不在运行下面的语句
                    }

                }
                //如果执行到这里说明找到用户名
                if (passWord.getText().equals("")) {
                    JOptionPane.showMessageDialog(frame, "密码不能为空,请重新输入");
                    return;

                } else {
                    if (passwordCheck.getText().equals(passWord.getText())) {
                        Account registeraccount = new Account(userName.getText(), passWord.getText(), "0");//实例化此账号
                        JOptionPane.showMessageDialog(frame, "注册成功,请登录");
                        Test.usersList.add(registeraccount);//加入Test类的静态用户list
                        Test.usersListUpdate();//更新用户文档

                        return;
                    } else {
                        JOptionPane.showMessageDialog(frame, "两次输入的密码不一致,请重新输入");
                        return;
                    }


                }
            }


            }
        if(e.getActionCommand().equals("     Login    ")){
            for (int i = 0; i < Test.usersList.size(); i++) {

                if (Test.usersList.get(i).id.equals(userName.getText())) {

                    if(passWord.getText().equals(Test.usersList.get(i).password))
                    {
                        JOptionPane.showMessageDialog(frame, "登录成功");
                        Test.currentAccount=Test.usersList.get(i);//将list中符合登陆输入的账户密码的类设为当前Test类中的静态的“当前类”,以便后面各种操作;
                        Test.file=new File(Test.currentAccount+".txt");
                        Test.recordString=new StringBuilder();//清空,避免将上一个用户的记录写进新登录的用户中
                        //Test.recordString.append("");//避免recordString空指针
                        Menu menu=new Menu();//实例化菜单窗口

                        Test.menu=menu;
                        frame.setVisible(false);//隐藏登陆窗口

                        /************************创建记录文件**********************/
                        File records = new File(Test.currentAccount.id+".txt");//以账户id命名
                        if(!records.exists())
                        {
                            try {
                                records.createNewFile();
                            }
                            catch (Exception e1)
                            {
                                JOptionPane.showMessageDialog(null, "创建该用户的记录失败");
                            }
                        }
                        Test.file=records;
                        /*****************读取记录文件************/
                        try {
                             fw = new FileReader(Test.file);//字符流
                        }
                        catch (Exception e1)
                        {
                            JOptionPane.showMessageDialog(null, "读取记录失败");
                        }
                        BufferedReader bfr=new BufferedReader(fw);

                        String temp="";
                        try {


                            while ((temp = bfr.readLine()) != null) {//不知为何读取出的字符串中最前面会出现Null,但不影响使用
                                Test.recordString .append(temp);//读取原先该账户的记录的每一行并拼接到Test.recordString中,在inqury类中输出作为查询记录的结果
                            }
                            //将记录读取并合并为一个字符串



                            fw.close();
                        }
                        catch (Exception e1)
                        {
                            System.out.println("读取记录过程中出现错误");
                        }


                        return;
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(frame, "密码错误");
                        passwordCheck.setText("");
                        return;
                    }

                }
            }
            JOptionPane.showMessageDialog(frame, "该用户不存在");


        }
        if(e.getActionCommand().equals("     Cancel    "))
        {
            p4.setVisible(false);
            login.setText("     Login    ");
            regirsterable=false;//不可注册
        }


    }
}


Menu类(菜单界面)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Menu implements ActionListener{
    public JFrame mframe;
    private JPanel mp0,mp1,mp2,mp3,mp4;//p4是确认密码;点击register按钮石出现


    private JTextField passWord,passwordCheck;
    private JButton inqury;
    private JButton outmoney;
    private JButton transfer;
    private JButton inmoney;
    private JButton changepassword;


    public Menu()
    {
        mframe=new JFrame();
        mframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton inqury=new JButton("查询");
        JButton outmoney=new JButton("取款");
        JButton transfer=new JButton("转账");
        JButton inmoney=new JButton("存款");
        JButton changepassword=new JButton("更改密码");
        JButton exit=new JButton("退卡");
        mp0=new JPanel();
        mp0.add(new JLabel("选择项目"));
        mframe.add(mp0);
        mp1=new JPanel();

        mp1.add(inmoney);
        mp1.add(inqury);
        mp1.add(outmoney);
        mp1.add(transfer);
        mp1.add(changepassword);
        mp1.add(exit);

        mp1.setLayout(new GridLayout(3,2,20,20));
        mframe.add(mp1);
        mframe.pack();
        mframe.setVisible(true);
        mframe.setLayout(new FlowLayout());
        mframe.setBounds(500,500,450,300);
        inqury.addActionListener(this);//绑定监听器
        inmoney.addActionListener(this);
        outmoney.addActionListener(this);
        transfer.addActionListener(this);
        changepassword.addActionListener(this);
        exit.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd=e.getActionCommand();//cmd赋值为点击的按钮的值
        if(cmd.equals("查询"))
        {
        Inqury inquryGui=new Inqury();
        }
        else if(cmd.equals("取款"))
        {
        OutMoney outMoneyGui=new OutMoney();
        }
        else if(cmd.equals("存款"))
        {
        InMoney inMoney=new InMoney();
        }else if(cmd.equals("转账"))
        {
        Transfer transfer=new Transfer();
        }else if(cmd.equals("更改密码"))
        {
        ChangePassword changePassword=new ChangePassword();
        }
        else if(cmd.equals("退卡")){

            mframe.setVisible(false);//隐藏
            LoginGui loginGui=new LoginGui();
            JOptionPane.showMessageDialog(null,"请记得取走你的银行卡");
        }
       
    }
}


InMoney类(存款界面)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;

public class InMoney implements ActionListener{
    public JTextField money;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public InMoney() {
        iframe=new JFrame("存款");

        ip0=new JPanel();
        ip0.add(new JLabel("账户id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("账户余额:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("存款金额:"));
        money=new JTextField(20);
        ip2.add(money);

        ip3=new JPanel();
        confirm=new JButton("确定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.add(cancel);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);//绑定监听器

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("确定"))//按下确定按钮
        {
            try {

                Test.currentAccount.inMoney(Integer.parseInt(money.getText()));//调用当前登陆账户的存钱函数

                JOptionPane.showMessageDialog(null, "存款成功");//弹窗
                yue.setText("账户余额:"+Test.currentAccount.money);
            }
            catch (ClassCastException e1)//捕获当前登录账户中inmoney函数中的异常。类型转换异常
            {

                JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");

            }
            catch (Exception e1)//
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
        iframe.setVisible(false);//隐藏

        }
    }
}


Inqury类(查询类)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Inqury implements ActionListener{
    public JFrame iframe;
    public JPanel ip0,ip1,ip2;
    public JTextArea inquryresult;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public Inqury() {
        iframe=new JFrame("查询");

        ip0=new JPanel();
        ip0.add(new JLabel("账户id:"+Test.currentAccount.id));
        ip1=new JPanel();
        yue=new JLabel("账户余额:"+Test.currentAccount.money);
        ip1.add(yue);
        ip2=new JPanel();
        inquryresult=new JTextArea(10,30);
        ip2.add(inquryresult);
        confirm=new JButton("查询记录");
        confirm.addActionListener(this);
        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("查询记录"));
        {
            //Test.recordString是从账户文档中度去处的字符串
            //在写入文本时/r/n才是换行,但在java中\r\n则是两个换行,而且Test.recordString是一行一行读取出来的拼接上的,所以并没有换行符,所以这里替换成一个\n
            inquryresult.setText(Test.recordString.toString().replace("元","元\n").replace("null",""));//去除掉结果字符串中的null,并将元替换为元\r\n来换行换行
            yue.setText("账户余额:"+Test.currentAccount.money);//更新显示余额
        }
    }
}

OutMoney类(取款)


package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;

public class OutMoney implements ActionListener{
    public JTextField money;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3;
    public JButton confirm,cancel,exit;
    public JLabel yue;//余额
    public OutMoney() {
        iframe=new JFrame("取款");

        ip0=new JPanel();
        ip0.add(new JLabel("账户id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("账户余额:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("取款金额:"));
        money=new JTextField(20);
        ip2.add(money);

        ip3=new JPanel();
        confirm=new JButton("确定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.add(cancel);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("确定"))//点击确定按钮
        {
            try {

                Test.currentAccount.outMoney(Integer.parseInt(money.getText()));

                JOptionPane.showMessageDialog(null, "取款成功");//弹窗
                yue.setText("账户余额:"+Test.currentAccount.money);//更新余额显示
            }
            catch (ClassCastException e1)
            {

                JOptionPane.showMessageDialog(null, "输入数据类型错误,请输入整数");//捕获Test类中outmoney方法的异常

            }
            catch (Exception e1)
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
            iframe.setVisible(false);//隐藏

        }
    }
}

Transfer类(转账)


package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer implements ActionListener{
    public JTextField money,other;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3,ip4;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public Transfer() {
        iframe=new JFrame("转账");

        ip0=new JPanel();
        ip0.add(new JLabel("账户id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("账户余额:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("转账账户id:"));
        other=new JTextField(10);
        ip2.add(other);

        ip4=new JPanel();
        ip4.add(new JLabel("转账金额:"));
        money=new JTextField(10);
        ip4.add(new JLabel("<html><br/><html>"));//换行
        ip4.add(money);

        ip3=new JPanel();
        confirm=new JButton("确定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(ip4);
        iframe.add(ip3);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("确定"))
        {
            try {

                Test.currentAccount.transfer(Integer.parseInt(money.getText()),other.getText());

                yue.setText("账户余额:"+Test.currentAccount.money);//更新面板上的余额
            }

            catch (Exception e1)
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
            iframe.setVisible(false);

        }
    }
}

ChangePassword类(更改密码)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePassword implements ActionListener{
    public JTextField oldPassword,newPassword,checkPassword;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3,ip4;
    public JButton confirm,cancel,exit;
    public ChangePassword() {
        iframe=new JFrame("更改密码");
        iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        ip2=new JPanel();
        ip2.add(new JLabel("原密码:"));
        oldPassword=new JTextField(20);
        ip2.add(new JLabel("<html><br/><html>"));//换行
        ip2.add(oldPassword);

        ip0=new JPanel();
        ip0.add(new JLabel("新密码:"));
        newPassword=new JTextField(20);
        ip0.add(new JLabel("<html><br/><html>"));//换行
        ip0.add(newPassword);

        ip4=new JPanel();
        ip4.add(new JLabel("再次输入新密码:"));
        checkPassword=new JTextField(20);
        ip4.add(new JLabel("<html><br/><html>"));//换行
        ip4.add(checkPassword);

        ip3=new JPanel();
        confirm=new JButton("确定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip2);
        iframe.add(ip0);
        iframe.add(ip4);
        iframe.add(ip3);
        iframe.add(confirm);
        iframe.add(cancel);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("确定")) {
            if (Test.currentAccount.password.equals(oldPassword.getText())) {
                try {
                    if(newPassword.getText().equals(checkPassword.getText())) {

                        Test.currentAccount.ChangePassword(newPassword.getText());
                        JOptionPane.showMessageDialog(null, "更改密码成功");
                        iframe.setVisible(false);
                        Test.menu.mframe.setVisible(false);//关闭菜单界面
                        new LoginGui();
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "两次输入的密码不一致");
                    }
                }
             catch (Exception e1) {//捕获账户类中更改密码函数的异常并弹窗显示
                    JOptionPane.showMessageDialog(null, e1.getMessage());
                }
            } else {

                JOptionPane.showMessageDialog(null, "原密码错误");
            }
        }
        else//如果点击cancel
        {
            iframe.setVisible(false);
        }
    }
}




本人是一名大二的学生,目前只学了一学期的java,因此有很多不规范的地方,请见谅。

这个程序技术含量并不高,在计划做的时候觉得很简单,但实际上写的时候,发现有一些细节逻辑比较难以处理和混乱,花费了我一周的时间,其中修复逻辑上的bug花了60%的时间,所以想发出来纪念一下。

我第一次写这么大的java程序,所以可能会有些混乱和有些多余的代码,但我详细的写了注释。希望能帮助到

需要帮助的朋友。若有问题,欢迎交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章