ATM机模拟系统
ATM机实现的功能
- 账户的登入
- 取钱
- 存钱
- 修改密码
- 查询余额
- 打印凭据
账户
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);
}
//抛出异常,由相关的界面类弹窗处理异常,下面几个方法同理
//如在取钱界面取钱,则会调用此函数,进行try/catch处理,获得这个函数的异常,弹窗说明异常
public void outMoney(int money) throws Exception{
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;
}
public void inMoney(int money)throws Exception{
try{
Writer 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"+Test.currentAccount.id+"\t存入"+money+"元\r\n").toString());
fw.flush();//写进文件
fw.close();
this.money+=money;
}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 temp1;
while((temp1=bfr.readLine())!=null){
temp1+=temp1;
}
temp1=temp1.replace("元","元\n\r")+dateString+"\t由"+Test.currentAccount.id+"\t转进"+money+"元\r\n";
System.out.println(temp1);
fw=new FileWriter(id+".txt");
fw.write(temp1);
fw.close();
JOptionPane.showMessageDialog(null,"转账成功");
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;
}
}
操作菜单
public class Menu{
public JFrame mframe;
private JLabel mp;
private JButton inqury;
private JButton outmoney;
private JButton transfer;
private JButton inmoney;
private JButton changepassword;
private JButton print;
private JButton exit;
Inqury inquryGui;
OutMoney outMoneyGui;
InMoney inMoney;
Transfer transferGui;
ChangePassword changePassword;
public Menu(){
makeframe();
}
public void makeframe(){
mframe = new JFrame("菜单");
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mframe.setLayout(null);
mp=new JLabel("请选择操作项目");
mp.setFont(new Font("楷体", Font.BOLD, 30));
mp.setForeground(Color.BLACK);
mp.setBounds(220, 5, 350, 30);
mframe.add(mp);
inqury=new JButton("查询");
inqury.setBorder(BorderFactory.createRaisedBevelBorder());
inqury.setBounds(20, 50, 100, 30);
mframe.add(inqury);
inqury.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Inqury();
}
});
outmoney=new JButton("取钱");
outmoney.setBorder(BorderFactory.createRaisedBevelBorder());
outmoney.setBounds(515, 50, 100, 30);
mframe.add(outmoney);
outmoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new OutMoney();
}
});
transfer=new JButton("转账");
transfer.setBorder(BorderFactory.createRaisedBevelBorder());
transfer.setBounds(20, 120, 100, 30);
mframe.add(transfer);
transfer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Transfer();
}
});
inmoney=new JButton("存钱");
inmoney.setBorder(BorderFactory.createRaisedBevelBorder());
inmoney.setBounds(515, 120, 100, 30);
mframe.add(inmoney);
inmoney.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new InMoney();
}
});
changepassword=new JButton("修改密码");
changepassword.setBorder(BorderFactory.createRaisedBevelBorder());
changepassword.setBounds(20, 190, 100, 30);
mframe.add(changepassword);
changepassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ChangePassword();
}
});
print=new JButton("打印凭据");
print.setBorder(BorderFactory.createRaisedBevelBorder());
print.setBounds(515, 190, 100, 30);
mframe.add(print);
print.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Print();
}
});
exit=new JButton("退出");
exit.setBorder(BorderFactory.createRaisedBevelBorder());
exit.setBounds(515, 300, 100, 30);
mframe.add(exit);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new LoginGui();
mframe.setVisible(false);//隐藏
JOptionPane.showMessageDialog(null,"请记得取走你的银行卡");
}
});
ImageIcon img = new ImageIcon("11.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(-19, 0,650 ,450);
mframe.add(jl);
mframe.setSize(650, 390);
mframe.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
mframe.setLocation(d.width/2-mframe.getWidth()/2,d.height/2-mframe.getHeight()/2);
}
public static void main(String[] args) {
new Menu();
}
}
存钱
public class InMoney{
public JTextField money;
public JFrame iframe;
public JButton confirm,cancel;
public JLabel ids,inmoney;
public InMoney(){
makeframe();
}
public void makeframe() {
iframe=new JFrame("存款");
iframe.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
iframe.setVisible(true);
iframe.setLayout(null);
inmoney=new JLabel("存款金额");
inmoney.setFont(new Font("微软雅黑", 0, 15));
inmoney.setForeground(Color.BLACK);
inmoney.setBounds(10, 92, 100, 15);
iframe.add(inmoney);
money=new JTextField(20);
money.setBounds(100, 90, 200, 25);
iframe.add(money);
confirm=new JButton("确定");
confirm.setBounds(30, 180, 100, 30);
iframe.add(confirm);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.inMoney(Integer.parseInt(money.getText()));//调用当前登陆账户的存钱函数
JOptionPane.showMessageDialog(null,"存款成功");//弹窗
}
catch(ClassCastException e1) {//捕获当前登录账户中inmoney函数中的异常。类型转换异常
JOptionPane.showMessageDialog(null,"输入数据类型错误,请输入整数");
}
catch (Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
cancel=new JButton("取消");
cancel.setBounds(205, 180, 100, 30);
iframe.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
iframe.setVisible(false);
}
});
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
iframe.add(jl);
iframe.setSize(350, 280);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
iframe.setLocation(d.width/2-iframe.getWidth()/2,d.height/2-iframe.getHeight()/2);
}
}
取钱
public class OutMoney{
public JTextField money;
public JFrame iframe;
public JButton money1,money2,money3,money5,money10,money20,money50,money100,cancel;
public JLabel title;//余额
public OutMoney(){
makeframe();
}
public void makeframe() {
iframe=new JFrame("取款");
iframe.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
iframe.setVisible(true);
iframe.setLayout(null);
title=new JLabel("请选择取款金额");
title.setFont(new Font("楷体", Font.BOLD, 30));
title.setForeground(Color.BLACK);
title.setBounds(220, 5, 350, 30);
iframe.add(title);
money1=new JButton("100");
money1.setBounds(20, 50, 100, 30);
iframe.add(money1);
money1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(100);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money2=new JButton("200");
money2.setBounds(515, 50, 100, 30);
iframe.add(money2);
money2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(200);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money3=new JButton("300");
money3.setBounds(20, 120, 100, 30);
iframe.add(money3);
money3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(300);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money5=new JButton("500");
money5.setBounds(515, 120, 100, 30);
iframe.add(money5);
money5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(500);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money10=new JButton("1000");
money10.setBounds(20, 190, 100, 30);
iframe.add(money10);
money10.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(1000);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money20=new JButton("2000");
money20.setBounds(515, 190, 100, 30);
iframe.add(money20);
money20.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(2000);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money50=new JButton("5000");
money50.setBounds(20, 260, 100, 30);
iframe.add(money50);
money50.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(5000);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
money100=new JButton("10000");
money100.setBounds(515, 260, 100, 30);
iframe.add(money100);
money100.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.outMoney(10000);
JOptionPane.showMessageDialog(null,"取款成功");//弹窗
iframe.setVisible(false);
}catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
});
cancel=new JButton("返回");
cancel.setBounds(515, 320, 100, 30);
iframe.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
iframe.setVisible(false);
}
});
ImageIcon img = new ImageIcon("11.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(-19, 0,650 ,450);
iframe.add(jl);
iframe.setSize(650 ,390);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
iframe.setLocation(d.width/2-iframe.getWidth()/2,d.height/2-iframe.getHeight()/2);
}
public static void main(String[] args) {
new OutMoney();
}
}
修改用户密码
public class ChangePassword{
public JTextField oldPassword,newPassword,checkPassword;
public JFrame iframe;
public JButton confirm,cancel,exit;
public JLabel mp2,opsd,npsd,repsd;
public ChangePassword(){
makeframe();
}
public void makeframe() {
iframe=new JFrame("更改密码");
iframe.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
iframe.setLayout(null);
opsd=new JLabel("原密码");
opsd.setFont(new Font("微软雅黑", 0, 15));
opsd.setForeground(Color.BLACK);
opsd.setBounds(30, 22, 100, 15);
iframe.add(opsd);
oldPassword=new JTextField(30);
oldPassword.setBounds(140, 20, 180, 25);
iframe.add(oldPassword);
npsd=new JLabel("新密码");
npsd.setFont(new Font("微软雅黑", 0, 15));
npsd.setForeground(Color.BLACK);
npsd.setBounds(30, 62, 100, 15);
iframe.add(npsd);
newPassword=new JTextField(30);
newPassword.setBounds(140, 60, 180, 25);
iframe.add(newPassword);
repsd=new JLabel("重输入新密码");
repsd.setFont(new Font("微软雅黑", 0, 15));
repsd.setForeground(Color.BLACK);
repsd.setBounds(30, 102, 100, 15);
iframe.add(repsd);
checkPassword=new JTextField(30);
checkPassword.setBounds(140, 100, 180, 25);
iframe.add(checkPassword);
confirm=new JButton("确定");
confirm.setBorder(BorderFactory.createRaisedBevelBorder());
confirm.setBounds(28, 180, 130, 30);
iframe.add(confirm);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
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,"原密码错误");
}
}
});
cancel=new JButton("取消");
cancel.setBorder(BorderFactory.createRaisedBevelBorder());
cancel.setBounds(180, 180, 130, 30);
iframe.add(cancel);
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
iframe.setVisible(false);
}
});
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
iframe.add(jl);
iframe.setVisible(true);
iframe.setSize(350, 280);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
iframe.setLocation(d.width/2-iframe.getWidth()/2,d.height/2-iframe.getHeight()/2);
}
public static void main(String[] args) {
new ChangePassword();
}
}
查询余额
public class Inqury{
public JFrame iframe;
public JTextArea inquryresult;
public JButton confirm,qu,cun,zhuan;
public JLabel yue,ids;
public Inqury(){
makeframe();
}
public void makeframe() {
iframe=new JFrame("查询");
iframe.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
iframe.setLayout(null);
iframe.setVisible(true);
ids=new JLabel("账户id:"+Test.currentAccount.id);
ids.setFont(new Font("楷体", 0, 25));
ids.setForeground(Color.GRAY);
ids.setBounds(70, 100, 200, 30);
iframe.add(ids);
yue=new JLabel("账户余额:"+Test.currentAccount.money);
yue.setFont(new Font("楷体", 0, 25));
yue.setForeground(Color.RED);
yue.setBounds(70, 160, 200, 30);
iframe.add(yue);
qu=new JButton("取钱");
qu.setBounds(20, 50, 100, 30);
iframe.add(qu);
qu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new OutMoney();
iframe.setVisible(false);
}
});
cun=new JButton("存钱");
cun.setBounds(220, 50, 100, 30);
iframe.add(cun);
cun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new InMoney();
iframe.setVisible(false);
}
});
zhuan=new JButton("转账");
zhuan.setBounds(20, 250, 100, 30);
iframe.add(zhuan);
zhuan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new Transfer();
iframe.setVisible(false);
}
});
confirm=new JButton("返回");
confirm.setBounds(220, 250, 100, 30);
iframe.add(confirm);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
iframe.setVisible(false);
}
});
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
iframe.add(jl);
iframe.setSize(350, 340);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
iframe.setLocation(d.width/2-iframe.getWidth()/2,d.height/2-iframe.getHeight()/2);
}
}
打印凭据
public class Print {
private JFrame frame;
public JPanel jp1,jp2;
private JTextArea text;
private JButton ok;
public Print() {
makeframe();
}
public void makeframe() {
frame=new JFrame("打印凭据");
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
frame.setVisible(true);
frame.setLayout(null);
text=new JTextArea(10,50);
text.setBounds(0, 0, 350, 280);
frame.add(text);
ok=new JButton("确定");
ok.setBounds(120, 280, 100, 30);
frame.add(ok);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setVisible(false);
}
});
text.setText("**********************************"+"\n"+
" 欢迎下次使用"+"\n"+
"账户:"+Test.currentAccount.id+"\n"+
"余额:"+Test.currentAccount.money+"\n"+
Test.recordString.toString().replace("元","元\n").replace("null","")
+"**********************************");
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
frame.add(jl);
frame.setSize(350, 350);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2-frame.getWidth()/2,d.height/2-frame.getHeight()/2);
}
}
转账
public class Transfer{
public JTextField money,other;
public JFrame iframe;
public JPanel ip0,ip1,ip2,ip3,ip4;
public JButton confirm,cancel,exit;
public JLabel yue,mp2,ids,tid,tyue;
public Transfer(){
makeframe();
}
public void makeframe() {
iframe=new JFrame("转账");
iframe.setIconImage(Toolkit.getDefaultToolkit().createImage("icbc.jpg"));
iframe.setVisible(true);
iframe.setLayout(null);
tid=new JLabel("转账账户id:");
tid.setFont(new Font("微软雅黑", 0, 15));
tid.setForeground(Color.BLACK);
tid.setBounds(10, 62, 100, 15);
iframe.add(tid);
other=new JTextField(20);
other.setBounds(100, 60, 200, 25);
iframe.add(other);
tyue=new JLabel("取款金额");
tyue.setFont(new Font("微软雅黑", 0, 15));
tyue.setForeground(Color.BLACK);
tyue.setBounds(10, 132, 100, 15);
iframe.add(tyue);
money=new JTextField(20);
money.setBounds(100, 130, 200, 25);
iframe.add(money);
confirm=new JButton("确定");
confirm.setBounds(30, 190, 100, 30);
iframe.add(confirm);
confirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
Test.currentAccount.transfer(Integer.parseInt(money.getText()),other.getText());
}
catch(Exception e1){
JOptionPane.showMessageDialog(iframe,"转账成功");
}
}
});
cancel=new JButton("取消");
cancel.setBounds(205, 190, 100, 30);
iframe.add(cancel);
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
iframe.setVisible(false);
}
});
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
iframe.add(jl);
iframe.setSize(350, 280);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
iframe.setLocation(d.width/2-iframe.getWidth()/2,d.height/2-iframe.getHeight()/2);
}
}
登入界面
public class LoginGui{
private JFrame frame;
private JLabel user,password,title;//p4包括确认密码时的输入框;点击register按钮出现
private JTextField users;
private JTextField passwords;
private JButton log,cannel;
private Reader fw;
Boolean regirsterable=true;//控制是否能注册的变量
public LoginGui(){
makeframe();
}
public void makeframe(){
frame = new JFrame("欢迎使用ATM机");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
title=new JLabel("商院ATM机模拟系统");
title.setFont(new Font("楷体", Font.BOLD, 30));
title.setForeground(Color.BLACK);
title.setBounds(28, 20, 350, 30);
frame.add(title);
user = new JLabel("用户名");
user.setFont(new Font("微软雅黑", 0, 14));
user.setForeground(Color.BLACK);
user.setBounds(30, 80, 50, 30);
frame.add(user);
users = new JTextField(10);
users.setBounds(120, 80, 180, 25);
frame.add(users);
password = new JLabel("密码");
password.setFont(new Font("微软雅黑", 0, 14));
password.setForeground(Color.BLACK);
password.setBounds(30, 120, 50, 30);
frame.add(password);
passwords = new JPasswordField(10);
passwords.setBounds(120, 120, 180, 25);
frame.add(passwords);
log = new JButton("登录");
log.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.YELLOW));
log.setBounds(20, 180, 130, 30);
frame.add(log);
log.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i=0;i<Test.usersList.size();i++){
if(Test.usersList.get(i).id.equals(users.getText())){
if(passwords.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();//清空,避免将上一个用户的记录写进新登录的用户中
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,"密码错误");
return;
}
}
}
JOptionPane.showMessageDialog(frame,"该用户不存在");
}
});
cannel = new JButton("取消");
cannel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.BLUE, Color.black));
cannel.setBounds(190,180, 130, 30);
cannel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
frame.add(cannel);
ImageIcon img = new ImageIcon("10.jpg");
JLabel jl = new JLabel(img);
jl.setBounds(2, -70,330,370);
frame.add(jl);
frame.setSize(350, 270);//设置大小
frame.setVisible(true);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2-frame.getWidth()/2,d.height/2-frame.getHeight()/2);
}
}
测试
public class Test {
public static ArrayList<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 Reader fw;
public static void main(String[] args)throws Exception{
try {
//UIManager 管理当前外观、可用外观集合、外观更改时被通知的 PropertyChangeListeners、外观默认值以及获取各种默认值的便捷方法。
//getSystemLookAndFeelClassName()如果有实现本机系统外观的 LookAndFeel 类的名称,则返回该名称;否则返回默认的跨平台 LookAndFeel 类的名称。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
usersList=new ArrayList<Account>();
/**********************用户文本**********************/
File users=new File("users.txt");
if(!users.exists()){
try{
users.createNewFile();
Writer fw = new FileWriter("users.txt");
fw.write("admin 12345 88888"+"\n");
fw.write("123456 123456 0");
fw.flush();
fw.close();
}catch(Exception e1){
JOptionPane.showMessageDialog(null,"创建用户文档失败");
}
}
usersFile=users;//创建用户文档,存储用户账户,密码,余额信息;
usersListRead();
//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]);
// System.out.println("读取到"+a.id);
usersList.add(a);
}
bfr.close();
fw.close();
}catch(Exception e){
System.out.println("读取用户文档失败");
}
}
}