package com.lovo.t80.t5.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import com.lovo.t80.t5.bean.Uesr;
import com.lovo.t80.t5.net.Client;
import com.lovo.t80.t5.util.DataManipulation;
public class AtmJFrame extends JFrame implements ActionListener{
public static AtmJFrame atm;
private JLabel label = new JLabel("欢迎光临东大银行ATM机");
private JPanel panel = new JPanel();
private JPanel cPanel = new JPanel();
private JPanel nPanel = new JPanel();
private JLabel label2 = new JLabel();
private JLabel label3 = new JLabel();
private JLabel label4 = new JLabel();
static private JButton registerButton = new JButton("注册帐号"); //注册
static private JButton loginButton = new JButton("用户登录"); //登录
static private JButton changePwdButton = new JButton("修改密码"); //修改密码
static private JButton depositButton = new JButton("存 款"); //存款
static private JButton drawButton = new JButton("取 款"); //取款
static private JButton queryButton = new JButton("查询余额"); //查询
static private JButton exitButton = new JButton("退出系统"); //退出
static private JButton cancelButton = new JButton("帐号注销"); //注销
public AtmJFrame() {
//----设置组件属性
label.setFont(new Font("Dialog",1,15));
label.setForeground(Color.red);
registerButton.setFont(new Font("Dialog",1,15));
registerButton.setForeground(Color.blue);
loginButton.setFont(new Font("Dialog",1,15));
loginButton.setForeground(Color.blue);
changePwdButton.setFont(new Font("Dialog",1,15));
changePwdButton.setForeground(Color.blue);
depositButton.setFont(new Font("Dialog",1,15));
depositButton.setForeground(Color.blue);
drawButton.setFont(new Font("Dialog",1,15));
drawButton.setForeground(Color.blue);
queryButton.setFont(new Font("Dialog",1,15));
queryButton.setForeground(Color.blue);
exitButton.setFont(new Font("Dialog",1,15));
exitButton.setForeground(Color.blue);
cancelButton.setFont(new Font("Dialog",1,15));
cancelButton.setForeground(Color.blue);
label.setHorizontalAlignment(SwingConstants.CENTER);
label2.setIcon(new ImageIcon("img/2.GIF"));
label3.setIcon(new ImageIcon("img/3.gif"));
label4.setIcon(new ImageIcon("img/1.jpg"));
changePwdButton.setEnabled(false);
depositButton.setEnabled(false);
drawButton.setEnabled(false);
queryButton.setEnabled(false);
exitButton.setEnabled(false);
cancelButton.setEnabled(false);
//----设置组件的位置
label2.setBounds(5, 10, 200, 100);
label3.setBounds(170, 10, 210, 100);
label4.setBounds(30, 10, 200, 400);
//----设置布局
panel.setLayout(new BorderLayout());
nPanel.setLayout(new GridLayout(2,4));
cPanel.setLayout(null);
//----将组件添加到容器
this.add(label,BorderLayout.NORTH);
this.add(panel,BorderLayout.CENTER);
panel.add(nPanel,BorderLayout.NORTH);
panel.add(cPanel,BorderLayout.CENTER);
nPanel.add(registerButton);
nPanel.add(loginButton);
nPanel.add(changePwdButton);
nPanel.add(depositButton);
nPanel.add(drawButton);
nPanel.add(queryButton);
nPanel.add(exitButton);
nPanel.add(cancelButton);
cPanel.add(label2);
cPanel.add(label3);
cPanel.add(label4);
//----创建容器
this.setTitle("automatic teller machine");
this.setBounds(330,150,400,450);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
//----添加监视器
registerButton.addActionListener(this);
loginButton.addActionListener(this);
changePwdButton.addActionListener(this);
depositButton.addActionListener(this);
drawButton.addActionListener(this);
queryButton.addActionListener(this);
exitButton.addActionListener(this);
cancelButton.addActionListener(this);
//----添加标识
registerButton.setActionCommand("register");
loginButton.setActionCommand("login");
changePwdButton.setActionCommand("changePwd");
depositButton.setActionCommand("deposit");
drawButton.setActionCommand("draw");
queryButton.setActionCommand("query");
exitButton.setActionCommand("exit");
cancelButton.setActionCommand("cancel");
}
String loginId; //记录登录的id
String loginPassword;
public void actionPerformed(ActionEvent arg0) {
String button = arg0.getActionCommand(); //获取标示
if(button == "register") { //注册
String id = JOptionPane.showInputDialog(this,"请输入用户名","");
String password = JOptionPane.showInputDialog(this,"请输入密码","");
Uesr uesr = new Uesr(id,password);
String str = "注册";
new Client(uesr,str);
} else if(button == "login") { //登录
loginId = JOptionPane.showInputDialog(this,"请输入用户名","");
loginPassword = JOptionPane.showInputDialog(this,"请输入密码","");
Uesr uesr = new Uesr(loginId,loginPassword);
String str = "登录";
new Client(uesr,str);
} else if(button == "changePwd") { //修改密码
String oldPassword = JOptionPane.showInputDialog(this,"请输入原始密码","");
String fNewPassword = JOptionPane.showInputDialog(this,"请输入新密码","");
String newPassword = JOptionPane.showInputDialog(this,"请输入新密码","");
boolean isB = DataManipulation.interpretation(oldPassword);
boolean isC = DataManipulation.interpretation(fNewPassword);
boolean isD = DataManipulation.interpretation(newPassword);
if(isB && isC && isD) {
Uesr uesr = new Uesr(loginId,oldPassword);
Client.alter(uesr, newPassword);
if(!fNewPassword.equals(newPassword)) {
JOptionPane.showMessageDialog(AtmJFrame.atm,"2次密码不一致","失败",JOptionPane.QUESTION_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AtmJFrame.atm,"密码不能为空","失败",JOptionPane.QUESTION_MESSAGE);
}
} else if(button == "exit") {
atm.dispose(); //关闭当前窗口
} else if(button == "cancel") {
Uesr uesr = new Uesr(loginId,loginPassword);
new Client(uesr,"注销");
} else if(button == "query") {
Uesr uesr = new Uesr(loginId,loginPassword);
new Client(uesr,"查询");
} else if(button == "deposit") {
Uesr uesr = new Uesr(loginId,loginPassword);
String money = JOptionPane.showInputDialog(this,"请输入你要存的金额","");
Client.deposit(uesr,money);
} else if(button == "draw") {
Uesr uesr = new Uesr(loginId,loginPassword);
String money = JOptionPane.showInputDialog(this,"请输入你要取的金额","");
Client.draw(uesr,money);
}
}
public static void display() {
registerButton.setEnabled(false);
loginButton.setEnabled(false);
changePwdButton.setEnabled(true);
depositButton.setEnabled(true);
drawButton.setEnabled(true);
queryButton.setEnabled(true);
exitButton.setEnabled(true);
cancelButton.setEnabled(true);
}
public static void cancel() {
registerButton.setEnabled(true);
loginButton.setEnabled(true);
changePwdButton.setEnabled(false);
depositButton.setEnabled(false);
drawButton.setEnabled(false);
queryButton.setEnabled(false);
exitButton.setEnabled(false);
cancelButton.setEnabled(false);
}
public static void main(String [] args) {
AtmJFrame.atm = new AtmJFrame();
}
}