热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

这是一个使用io流和swing库制作的可存储的窗口化ATM机程序;臭不要脸的发上来实现的功能有:1.登录和注册用户(虽然现实中的ATM中没有注册功能)2.存款3.取款4.查询记录

这是一个使用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 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();

//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 // 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 {
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
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
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("
"));//换行
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("
"));//换行
ip2.add(oldPassword);

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

ip4=new JPanel();
ip4.add(new JLabel("再次输入新密码:"));
checkPassword=new JTextField(20);
ip4.add(new JLabel("
"));//换行
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程序,所以可能会有些混乱和有些多余的代码,但我详细的写了注释。希望能帮助到

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


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • 本文介绍了在PostgreSQL中批量导入数据时的优化方法。包括使用unlogged表、删除重建索引、删除重建外键、禁用触发器、使用COPY方法、批量插入等。同时还提到了一些参数优化的注意事项,如设置effective_cache_size、shared_buffer等,并强调了在导入大量数据后使用analyze命令重新收集统计信息的重要性。 ... [详细]
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼*madebyebhrz*#include#include#include#include#include#include#include ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • Java图形化计算器设计与实现
    本文介绍了使用Java编程语言设计和实现图形化计算器的方法。通过使用swing包和awt包中的组件,作者创建了一个具有按钮监听器和自定义界面尺寸和布局的计算器。文章还分享了在图形化界面设计中的一些心得体会。 ... [详细]
  • 使用freemaker生成Java代码的步骤及示例代码
    本文介绍了使用freemaker这个jar包生成Java代码的步骤,通过提前编辑好的模板,可以避免写重复代码。首先需要在springboot的pom.xml文件中加入freemaker的依赖包。然后编写模板,定义要生成的Java类的属性和方法。最后编写生成代码的类,通过加载模板文件和数据模型,生成Java代码文件。本文提供了示例代码,并展示了文件目录结构。 ... [详细]
  • 流数据流和IO流的使用及应用
    本文介绍了流数据流和IO流的基本概念和用法,包括输入流、输出流、字节流、字符流、缓冲区等。同时还介绍了异常处理和常用的流类,如FileReader、FileWriter、FileInputStream、FileOutputStream、OutputStreamWriter、InputStreamReader、BufferedReader、BufferedWriter等。此外,还介绍了系统流和标准流的使用。 ... [详细]
  • tcpdump 4.5.1 crash 深入分析
    tcpdump 4.5.1 crash 深入分析 ... [详细]
  • STM32 IO口模拟串口通讯
    转自:http:ziye334.blog.163.comblogstatic224306191201452833850647前阵子,调项目时需要用到低波 ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
author-avatar
手机用户2502929965
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有