您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > ATM程序java实现(客户端+服务器)
dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405程序实现其实并不是很难,主要是思路,对程序的各个功能的设计,各个按钮的处理事件及按钮按照逻辑显示出来和隐藏。服务器采用多线程并行机制,针对每个用户登录创建一个线程。程序代码客户端:importjavax.swing.*;importjava.net.*;importjava.io.*;importjava.util.*;importjava.awt.event.*;importjava.awt.*;importjava.sql.*;importjava.math.*;importjavax.swing.*;classATMWindowsextendsJFrameimplementsActionListener{Fontf;Socketclint=null;DataOutputStreamout=null;DataInputStreamin=null;Connectioncon;Statementpre;ResultSetrs;inti,money;booleanb=true;//b的取值有0,1,共2个值分别告诉程序该吧数字键盘的输入复制给username,password,Stringsusername=null;Stringspassword=null;Boxbox1,box2,box3,box11,box12;JPanelpanel1,panel2;JLabeltishi;JTextFieldusername;JPasswordFieldpassword;JButtonn[],queding,chaxun,qukuan,tuichu;ATMWindows(Strings){GraphicsEnvironmentge=GraphicsEnvironment.getLocalGraphicsEnvironment();Stringt[]=ge.getAvailableFontFamilyNames();dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405f=newFont(t[67],Font.BOLD,16);n=newJButton[10];box1=Box.createVerticalBox();box2=Box.createHorizontalBox();box3=Box.createVerticalBox();panel1=newJPanel();panel2=newJPanel();tishi=newJLabel(欢迎使用XX银行!请输入卡号。);tishi.setFont(f);username=newJTextField(20);password=newJPasswordField(20);panel1.setLayout(newGridLayout(3,3));panel2.setLayout(newGridLayout(4,1));for(i=1;in.length;i++){n[i]=newJButton(+i);panel1.add(n[i]);n[i].addActionListener(this);}n[0]=newJButton(0);panel1.add(n[0]);n[0].addActionListener(this);n[0].setPreferredSize(newDimension(20,20));queding=newJButton(确定);chaxun=newJButton(查询);//用户输入账号和密码后,点击查询按钮登陆qukuan=newJButton(取款);tuichu=newJButton(退出);qukuan.show(false);chaxun.show(false);//初始化窗口后,账号和密码还没输入,不能用查询按钮登陆box1.add(tishi);tishi.setBounds(50,10,290,60);box1.add(username);username.setBounds(50,60,300,20);password.setBounds(60,60,300,20);panel2.add(queding);panel2.add(chaxun);panel2.add(qukuan);panel2.add(tuichu);dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405box1.setBounds(50,10,300,60);n[0].setBounds(50,290,200,70);panel1.setBounds(50,80,200,210);panel2.setBounds(250,80,100,280);add(box1);add(panel1);add(panel2);add(n[0]);tuichu.addActionListener(this);queding.addActionListener(this);chaxun.addActionListener(this);qukuan.addActionListener(this);setLayout(null);setTitle(s);setBounds(200,200,400,400);validate();setResizable(false);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}publicvoidactionPerformed(ActionEvente){if(e.getSource()==tuichu)//用户退出,窗口不关闭,需要从新输入账号和密码{b=true;//用户推出之后,从数字键输入到usernamebox1.add(username);box1.remove(password);box1.validate();username.setText(null);money=0;susername=null;spassword=null;tishi.setText(欢迎使用XX银行!请输入卡号。);chaxun.show(false);queding.show(true);qukuan.show(false);//用户退出,查询和取款按钮不可见if(clint!=null){dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405try{out.writeInt(-1);//服务器输入数字-1,表示客户已经退出,服务器可以关闭该客户的进程}catch(IOExceptione1){System.out.println(e1);}}}elseif(e.getSource()==queding){if(susername==null){susername=username.getText();username.setText(null);tishi.setText(请输入密码);b=false;//从数字键输入到passwordbox1.remove(username);box1.add(password);box1.validate();password.setText(null);}elseif(spassword==null){spassword=password.getText();chaxun.show(true);b=true;//从数字键输入到usernamequeding.show(false);tishi.setText(请点击查询按钮登陆!);password.setText(null);}else{intyue;try{dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405BigIntegern=newBigInteger(username.getText());yue=money-n.intValue();if(yue=0&&n.intValue()0)//余额大于等于0,表示用户可以取款{out.writeInt(n.intValue());money=in.readInt();if(money=yue){tishi.setText(取款成功,您的余额为:+money);queding.show(false);username.setText(null);box1.validate();}elseif(money==-3)//服务器连接数据库错误,取款失败{money=yue;tishi.setText(抱歉!服务器错误,取款失败。);username.setText(null);box1.validate();qukuan.show(false);queding.show(false);validate();}else{;queding.show(false);tishi.setText(操作失败,您的余额为:+money);}username.setText(null);box1.remove(username);box1.validate();qukuan.show(true);validate();}else{tishi.setText(输入错误,不能大于您的余额+money);dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405qukuan.show(true);queding.show(false);chaxun.show(false);username.setText(null);validate();}}catch(IOExceptione1){tishi.setText(输入错误,不能大于您的余额+money);qukuan.show(true);queding.show(false);chaxun.show(false);username.setText(null);validate();System.out.println(e1);}}}elseif(e.getSource()==qukuan){tishi.setText(请输入取款金额然后点击确定。);box1.remove(password);box1.add(username);box1.validate();box1.remove(username);box1.add(username);box1.validate();username.setText(null);qukuan.show(false);queding.show(true);}elseif(e.getSource()==chaxun){try{clint=newSocket(localhost,4333);in=newDataInputStream(clint.getInputStream());dmjlsl原创,仅供交流。如有疑问,或指点,请联系QQ:403310405out=newDataOutputStream(clint.getOutputStream());tishi.setText(连接到服务器);}catch(IOExceptione1){tishi.setText(无法连接到服务器!);}try{out.writeUTF(susername);out.writeUTF(spassword);money=in.readInt();if(money=0){tishi.setText(登陆成功,您当前余额为:+money);chaxun.show(false);qukuan.show(true);box1.remove(password);box1.validate();return;}elseif(money==-1){tishi.setText(卡号错误请从新输入卡号和密码);susername=null;spassword=null;box1.remove(password);
本文标题:ATM程序java实现(客户端+服务器)
链接地址:https://www.777doc.com/doc-5064072 .html