atm_client.java
来自「简单ATM(Automatic Teller Machine)系统的基本功能包括」· Java 代码 · 共 544 行 · 第 1/2 页
JAVA
544 行
package atm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.io.*;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
public class ATM_client extends WindowAdapter implements ActionListener
{ Frame winOperate,winLogin;
MenuBar myMenu;
Menu fileMenu,helpMenu;
MenuItem fileMenuItem1,helpMenuItem1;
MyDialog dialogAbout;Dialog dialogErr;
TextField tfUser,tfPwd;
Panel plLogin=new Panel();Panel plMenu=new Panel();Panel plOperate=new Panel();
Label logo,lblErr;
Button btnLogin,btnCancel,btnBalance,btnDeposit,btnWithdraw,btnChange,btnExit,btnOK;
String username;
String password;
boolean flag=false;
String url="jdbc:odbc:account";
private static Socket socket;
private static BufferedReader is;
private static PrintStream out;
private static String SERVER_HOST = "localhost";
ATM_client(){
winLogin=new Frame("登录");
winOperate=new Frame("简易自动取款机");
winOperate.setLayout(new BorderLayout());
winLogin.setLayout(new BorderLayout());
//设置菜单
myMenu=new MenuBar();
fileMenu=new Menu("文件");
helpMenu=new Menu("帮助");
myMenu.add(fileMenu);
myMenu.add(helpMenu);
fileMenuItem1=new MenuItem("退出");
helpMenuItem1=new MenuItem("关于");
myMenu.add(fileMenu);
myMenu.add(helpMenu);
fileMenu.add(fileMenuItem1);
helpMenu.add(helpMenuItem1);
fileMenuItem1.addActionListener(new ActionListener1());
helpMenuItem1.addActionListener(new ActionListener1());
winOperate.setMenuBar(myMenu); //或winOperate.add(myMenu)
//设置对话框(帮助菜单)
dialogAbout=new MyDialog(winOperate,"关于...",true);//必须得先关
dialogAbout.addWindowListener(this);
dialogErr=new Dialog(winLogin,"交易失败!",true);//必须得先关
dialogErr.addWindowListener(this);
//用户名及密码
tfUser=new TextField(10);
tfUser.setBackground(Color.yellow);
tfUser.setForeground(Color.blue);
tfPwd=new TextField(10);
tfPwd.setEchoChar('*');
tfPwd.setBackground(Color.yellow);
tfPwd.setForeground(Color.blue);
//设置各个Button
winLogin.add(plLogin,BorderLayout.CENTER);
plLogin.setLayout(new FlowLayout());
plLogin.setBackground(Color.red);
plLogin.add(new Label(" 帐号和密码必须由六位数字组成"));
plLogin.add(new Label("用户名:"));
plLogin.add(tfUser);
plLogin.add(new Label("密 码: "));
plLogin.add(tfPwd);
btnLogin=new Button("登录");
btnCancel=new Button("清除");
btnLogin.addActionListener(new ActionListener1());
btnCancel.addActionListener(new ActionListener1());
btnBalance=new Button("余额");
btnBalance.setForeground(Color.black);
btnBalance.setBackground(Color.WHITE);
btnBalance.addActionListener(new ActionListener1());
btnWithdraw=new Button("取款");
btnWithdraw.setForeground(Color.black);
btnWithdraw.setBackground(Color.WHITE);
btnWithdraw.addActionListener(new ActionListener1());
btnDeposit=new Button("存款");
btnDeposit.setForeground(Color.black);
btnDeposit.setBackground(Color.WHITE);
btnDeposit.addActionListener(new ActionListener1());
btnChange=new Button("改密");
btnChange.setForeground(Color.black);
btnChange.setBackground(Color.WHITE);
btnChange.addActionListener(new ActionListener1());
btnExit=new Button("退出");
btnExit.setForeground(Color.black);
btnExit.setBackground(Color.WHITE);
btnExit.addActionListener(new ActionListener1());
btnOK=new Button("确定");
//btnOK.addActionListener(new ActionListener1());
btnOK.addActionListener(this);
plLogin.add(btnLogin);
plLogin.add(btnCancel);
winOperate.add(plMenu,BorderLayout.EAST);winOperate.add(plOperate,BorderLayout.CENTER);
plMenu.setLayout(new GridLayout(5,1));plMenu.setBackground(Color.red);
plMenu.add(btnBalance);
plMenu.add(btnWithdraw);
plMenu.add(btnDeposit);
plMenu.add(btnChange);
plMenu.add(btnExit);
logo=new Label(" 欢迎使用ATM系统,请选择操作!");
logo.setFont(new Font("Serif",Font.PLAIN,15));
lblErr=new Label("注意:帐号和密码由六位数字组成,单次交易金额不超过2000元!");
lblErr.setBackground(Color.red);lblErr.setForeground(Color.blue);
dialogErr.add(lblErr);dialogErr.setBackground(Color.yellow);
dialogErr.add(btnOK);
dialogErr.setSize(400, 100);
dialogErr.setLayout(new FlowLayout());
winOperate.add(logo,BorderLayout.NORTH);winOperate.setBackground(Color.red);
winOperate.setSize(600,400);winLogin.setSize(190,180);
winOperate.addWindowListener(this);winLogin.addWindowListener(this);
//设置窗体位置,居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = winOperate.getSize();
winOperate.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frameSize = winLogin.getSize();
winLogin.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
winOperate.setVisible(false);winLogin.setVisible(true);
frameSize = dialogErr.getSize();
dialogErr.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frameSize = dialogAbout.getSize();
dialogAbout.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
public static void main(String args[])
{
try
{
if(args.length>0)
SERVER_HOST=args[0];
socket = new Socket(SERVER_HOST, Msg.PORTNUM);
// Set up input and output stream filters.
is =new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
//显示logo图片
new ATM_client();
Thread thread;
Logo lg = new Logo("logo.gif");
thread = new Thread(lg);
thread.start();
}
catch(Exception e)
{
//System.out.println(e.toString());
}
}
public void windowClosing(WindowEvent e)
{
if(e.getSource()==winOperate)
{
winOperate.dispose();
winLogin.setVisible(true);
tfUser.setText("");
tfPwd.setText("");
}
if(e.getSource()==winLogin)
{
exitPro();
//System.exit(0);
}
if(e.getSource()==dialogAbout)
{
dialogAbout.dispose();
}
if(e.getSource()==dialogErr)
{
dialogErr.dispose();
}
}
public void exitPro()
{
// Close streams and socket.
try{
is.close();
out.close();
socket.close();
}catch(IOException e){
//System.out.println(e.toString());
}
System.exit(0);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getActionCommand().equals("确定"))
dialogErr.dispose();
tfPwd.setText("");
}
public void login(String account,String pwd)
{
try
{
//check account and password
if(Msg.isValid(account,Msg.ACCOUNT_LENGTH)
&& Msg.isValid(pwd,Msg.PWD_LENGTH))
{
String msg=Character.toString(Msg.CMD_LOGIN)
+ account +Character.toString(Msg.SEPARATOR) + pwd;
out.println(msg);
String line;
if ((line = is.readLine()) != null) {
System.out.println("Received: "+line);
char cmd = line.charAt(0);
if (cmd == Msg.CMD_LOGIN) {
char result = line.charAt(1);
if (result == Msg.SUCCESSFUL) { //登录成功
flag = true;
plOperate.removeAll();
plOperate.setBackground(Color.red);
winOperate.setVisible(true);
winLogin.setVisible(false);
return;
}
}
}
}
dialogErr.setVisible(true);
}
catch(Exception ex)
{
//System.out.println(ex.toString());
}
}
public void queryBalance()
{
try {
//check account and password
String msg=Character.toString(Msg.CMD_QUERY)
+Character.toString(Msg.SEPARATOR);
out.println(msg);
String line;
if ((line = is.readLine()) != null) {
System.out.println("Received: "+line);
char cmd = line.charAt(0);
if (cmd == Msg.CMD_QUERY) {
String balance = line.substring(1);
if (balance != Character.toString(Msg.FAILED)) { //成功
plOperate.removeAll();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?