import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainFrame extends JFrame implements ActionListener {
String name;
Container cn;
JLabel jl1, jlCheck, jlWelcome, jladd, jlend, jlWith;
JPanel jp1eft, cards, cardWelcome, cardDispose, cardWithdraw, cardCheck;
JButton jbck, jbadd, jbDis, jbExit, jbDisAdd, jbDisEsc, jbWithAdd,
jbWithEsc;
JTextField jtfadd, jtfWith;
Font f1, f2,f3;
MainFrame(String name) {
this.name = name;
this.setTitle("自动存取款机--用户");
this.setSize(620, 420);
this.setLocation(300, 130);
JFrame.setDefaultLookAndFeelDecorated(true);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void showMainFrame() {
cn = this.getContentPane();
cn.setLayout(new BorderLayout(10, 22));
cn.setBackground(Color.PINK);
f1 = new Font("黑体", Font.BOLD, 38);
f2 = new Font("宋体", Font.BOLD, 24);
f3 = new Font("楷体", Font.BOLD, 16);
jl1 = new JLabel("自动存取款机", JLabel.CENTER);
jl1.setFont(f1);
// ImageIcon img = new ImageIcon("log.jpg");
// jl1.setIcon(img);
jlend = new JLabel(name+"用户,你好,请注意周围环境是否安全,如有问题请拨打客服电话。", JLabel.CENTER); // 界面最后一行
jlend.setFont(f3);
cn.add(jl1, BorderLayout.NORTH);
cn.add(jlend, BorderLayout.SOUTH);
jp1eft = new JPanel();
jp1eft.setLayout(new GridLayout(0, 1, 10, 10));
jp1eft.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
cn.add(jp1eft, BorderLayout.WEST);
jbck = new JButton("查询余额");
jbadd = new JButton(" 存 款 ");
jbDis = new JButton(" 取 款 ");
jbExit = new JButton("退出系统");
jbck.setFont(f2);
jbadd.setFont(f2);
jbDis.setFont(f2);
jbExit.setFont(f2);
jp1eft.add(jbck);
jp1eft.add(jbadd);
jp1eft.add(jbDis);
jp1eft.add(jbExit);
cards = new JPanel(new CardLayout());
// 欢迎界面卡片的创建
cardWelcome = new JPanel();
jlWelcome = new JLabel("欢迎使用ATM,请选择操作的项目!");
jlWelcome.setFont(f2);
cardWelcome.add(jlWelcome);
cardWelcome.setName("欢迎");
// cardCheck,查询余额卡片的创建
cardCheck = new JPanel();
jlCheck = new JLabel("您的余额为:!");
jlCheck.setFont(f2);
cardCheck.add(jlCheck);
cardCheck.setName("查询");
// cardDispose,存款卡片的创建
cardDispose = new JPanel(new BorderLayout(5, 20));
jladd = new JLabel("请输入存款金额:");
jladd.setForeground(Color.blue);
jladd.setFont(f2);
jtfadd = new JTextField(15);
jtfadd.setFont(f2);
jbDisAdd = new JButton("存款");
jbDisEsc = new JButton("取消");
jbDisAdd.setFont(f2);
jbDisAdd.setForeground(Color.red);
jbDisEsc.setFont(f2);
cardDispose.add(jladd, BorderLayout.NORTH);
cardDispose.add(jtfadd);
JPanel jp1 = new JPanel();
jp1.add(jbDisAdd);
jp1.add(jbDisEsc);
cardDispose.add(jp1, BorderLayout.SOUTH);
// cardDispose.add(jbDisEsc);
cardDispose.setName("存款");
// cardWithdraw,取款卡片的创建 jbWithAdd,jbWithEsc,jtfWith,jlWith;
cardWithdraw = new JPanel(new BorderLayout(5, 20));
jlWith = new JLabel("请输入取款金额:");
jlWith.setForeground(Color.green);
jlWith.setFont(f2);
jtfWith = new JTextField(15);
jtfWith.setFont(f2);
jbWithAdd = new JButton("取款");
jbWithEsc = new JButton("取消");
jbWithAdd.setFont(f2);
jbWithAdd.setForeground(Color.red);
jbWithEsc.setFont(f2);
cardWithdraw.add(jlWith, BorderLayout.NORTH);
cardWithdraw.add(jtfWith);
JPanel jp2 = new JPanel();
jp2.add(jbWithAdd);
jp2.add(jbWithEsc);
cardWithdraw.add(jp2, BorderLayout.SOUTH);
// cardDispose.add(jbDisEsc);
cardWithdraw.setName("取款");
// 将各个卡片添加到cards中
cards.add(cardWelcome, "欢迎");
cards.add(cardCheck, "查询");
cards.add(cardDispose, "存款");
cards.add(cardWithdraw, "取款");
cn.add(cards, BorderLayout.CENTER);
// 注册事件监听
jbck.addActionListener(this);
jbadd.addActionListener(this);
jbDis.addActionListener(this);
jbDisAdd.addActionListener(this);// 存款界面的”确定“按钮
jbDisEsc.addActionListener(this);// 存款界面的”取消“按钮
jbWithAdd.addActionListener(this);// 取款界面的”确定“按钮
jbWithEsc.addActionListener(this);
jbExit.addActionListener(this);
}
// public static void main(String[] args) {
// new MainFrame("01").showMainFrame();
// }
@Override
public void actionPerformed(ActionEvent e) {
Jdbc jdbc = new Jdbc();
ResultSet rs;
int maneyY = 0;
CardLayout c1 = (CardLayout) cards.getLayout();
if (e.getSource() == jbck) {
c1.show(cards, "查询");
String sql = "Select * From user where CountNumber='" + name + "'";
rs = jdbc.sel(sql);
try {
rs.next();
maneyY = rs.getInt("Maney");
jlCheck.setText("您的余额为:" + maneyY + "元整!");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
jdbc.closeSqlConn();
} else if (e.getSource() == jbadd) {
c1.show(cards, "存款");
}
else if (e.getSource() == jbDis)
c1.show(cards, "取款");
else if (e.getSource() == jbDisAdd) {// 存款界面的”确定“按钮
String sql = "Select * From user where CountNumber='" + name + "'";
rs = jdbc.sel(sql);
try {
rs.next();
maneyY = rs.getInt("Maney");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
int maneyX = Integer.parseInt(jtfadd.getText().trim());
int maney = maneyY + maneyX;
sql = "UPDATE user SET Maney=" + maney + " WHERE CountNumber='"
+ name + "'";
int n = jdbc.updataStu(sql);
String str = "存入本帐户" + maneyX + "元整!";
if (n == 1) {
JOptionPane.showMessageDialog(null, str, null,
JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null, "存款失败!", null,
JOptionPane.ERROR_MESSAGE);
} catch (NumberFormatException exe) {
JOptionPane.showMessageDialog(null, "存款金额输入错误!", null,
JOptionPane.ERROR_MESSAGE);
}
jtfadd.setText("");
} else if (e.getSource() == jbWithAdd) {// 取款界面的”确定“按钮
String sql = "Select * From user where CountNumber='" + name + "'";
rs = jdbc.sel(sql);
try {
rs.next();
maneyY = rs.getInt("Maney");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
int maneyX = Integer.parseInt(jtfWith.getText().trim());
if (maneyX > maneyY) {
JOptionPane.showMessageDialog(null, "余额不足!", null,
JOptionPane.ERROR_MESSAGE);
} else {
int maney = maneyY - maneyX;
sql = "UPDATE user SET Maney=" + maney
+ " WHERE CountNumber='" + name + "'";
int n = jdbc.updataStu(sql);
String str = "从本帐户取款" + maneyX + "元整!";
if (n == 1) {
JOptionPane.showMessageDialog(null, str, null,
JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null, "存款失败!", null,
JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException exe) {
JOptionPane.showMessageDialog(null, "取款金额输入错误!", null,
JOptionPane.ERROR_MESSAGE);
}
jtfWith.setText("");
} else if (e.getSource() == jbExit) {
jdbc.closeSqlConn();
System.exit(0);
} else if (e.getSource() == jbDisEsc) {
jtfadd.setText("");
}
else if (e.getSource() == jbWithEsc) {
jtfWith.setText("");
}
}
}