📄 atm.java
字号:
cs.setString(1, username);
ResultSet rs = cs.executeQuery();
while (rs.next()) {
int balance = rs.getInt("balance");
myP.removeAll();
myP4.removeAll();
Label account = new Label("你的余额为:");
TextField tx3 = new TextField(15);
tx3.setText("" + balance);
tx3.setEditable(false);
tx3.setBackground(Color.WHITE);
myP.add(account, BorderLayout.NORTH);
myP.add(tx3, BorderLayout.WEST);
// /
JLabel la001 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("mn.jpg");
la001.setIcon(new ImageIcon(imgURLE));
myP4.add(la001);
win1.add(myP4, BorderLayout.CENTER);
// /
win1.setVisible(true);
}
cs.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException caught");
} catch (ClassNotFoundException ex) {
System.out.println("error");
}
}
public void takeMoney(int money) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
+ defaultPath + "\\data\\data.mdb";
Connection con = DriverManager.getConnection(dburl, "", "");
CallableStatement cs = con.prepareCall("{call query1(?)}");
cs.setString(1, username);
ResultSet rs = cs.executeQuery();
while (rs.next()) {
int balance = rs.getInt("balance");
if (money <= balance) {
CallableStatement cs1 = con.prepareCall("{call query2(?)}");
cs1.setInt(1, money);
cs1.executeUpdate();
cs1.close();
myP.removeAll();
myP4.removeAll();
Label money1 = new Label("交易成功!", Label.CENTER);
myP.add(money1);
// //
JLabel la0011 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("qqs.jpg");
la0011.setIcon(new ImageIcon(imgURLE));
myP4.add(la0011);
win1.add(myP4, BorderLayout.CENTER);
// //
win1.setVisible(true);
}
}
cs.close();
con.close();
} catch (SQLException e) {
balance();
} catch (ClassNotFoundException ex) {
System.out.println("error");
}
}
public void updatePassword(int pass) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
+ defaultPath + "\\data\\data.mdb";
Connection con = DriverManager.getConnection(dburl, "", "");
CallableStatement cs = con.prepareCall("{call query1(?)}");
cs.setString(1, username);
ResultSet rs = cs.executeQuery();
while (rs.next()) {
CallableStatement cs2 = con.prepareCall("{call query3(?)}");
cs2.setInt(1, pass);
cs2.executeUpdate();
cs2.close();
myP.removeAll();
myP4.removeAll();
Label pass1 = new Label("密码修改成功", Label.CENTER);
pass1.setFont(new Font("Serif", Font.BOLD, 16));
myP.add(pass1);
// //
JLabel la00119 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("gms.jpg");
la00119.setIcon(new ImageIcon(imgURLE));
myP4.add(la00119);
win1.add(myP4, BorderLayout.CENTER);
// //
win1.setVisible(true);
}
cs.close();
con.close();
} catch (SQLException e) {
balance();
} catch (ClassNotFoundException ex) {
System.out.println("error");
}
}
class ActionListener1 implements ActionListener {
int money3, password1;
Button btn7, btn8;
String x, y;
TextField tx5, tx6;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fileMenuItem1) {
System.exit(0);
}
if (e.getSource() == helpMenuItem1) {
dialog1.setSize(200, 100);
dialog1.setVisible(true);
}
if (e.getSource() == btn6) {
if (flag == true) {
myP.removeAll();
win1.dispose();
System.exit(0);
}
}
if (e.getSource() == btn2) {
tx1.setText("");
tx2.setText("");
}
if (e.getSource() == btn1) {
username = tx1.getText();
password = Integer.parseInt(tx2.getText());
login(username, password);
}
if (e.getSource() == btn3) {
if (flag == true) {
myP.removeAll();
balance();
}
}
if (e.getSource() == btn4) {
if (flag == true) {
myP.removeAll();
myP4.removeAll();
// ///
Panel pp00 = new Panel();
pp00.setLayout(new FlowLayout());
Panel pp11 = new Panel();
pp11.setLayout(new FlowLayout());
// ///
Label money2 = new Label("输入多少钱:");
tx5 = new TextField(10);
pp00.add(money2);
pp00.add(tx5);
btn7 = new Button("OK");
btn7.addActionListener(new ActionListener2());
pp11.add(btn7);
myP.add(pp00, BorderLayout.NORTH);
myP.add(pp11, BorderLayout.CENTER);
// //
JLabel la00011 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("quqian.jpg");
la00011.setIcon(new ImageIcon(imgURLE));
myP4.add(la00011);
win1.add(myP4, BorderLayout.CENTER);
// //
win1.setVisible(true);
}
}
if (e.getSource() == btn5) {
if (flag == true) {
myP.removeAll();
myP4.removeAll();
// ///
Panel pp000 = new Panel();
pp000.setLayout(new FlowLayout());
// ///
Label pass3 = new Label("输入新密码:");
tx6 = new TextField(10);
tx6.setEchoChar('*'); // 设置此文本字段的回送字符
pp000.add(pass3);
pp000.add(tx6);
btn8 = new Button("OK");
btn8.addActionListener(new ActionListener2());
pp000.add(btn8);
// pp111.add(btn8);
myP.add(pp000, BorderLayout.NORTH);
// myP.add(pp111,BorderLayout.EAST);
// ////
JLabel la000111 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("gm.jpg");
la000111.setIcon(new ImageIcon(imgURLE));
myP4.add(la000111);
win1.add(myP4, BorderLayout.CENTER);
win1.setVisible(true);
}
}
}
class ActionListener2 implements ActionListener {
public void actionPerformed(ActionEvent ee) {
if (ee.getSource() == btn7) {
x = tx5.getText();
money3 = Integer.parseInt(x);
takeMoney(money3);
}
if (ee.getSource() == btn8) {
y = tx6.getText();
password1 = Integer.parseInt(y);
updatePassword(password1);
}
}
}
}
class MyDialog extends Dialog {
private static final long serialVersionUID = 1L;
Label label1 = new Label("ATM升级版本");
Label label3 = new Label("生产日期(Date):2007~12~22");
Label label2 = new Label("Author:Dinitial~~All Rights Reserved");
MyDialog(Frame frame1, String title, boolean bool) {
super(frame1, title, bool);
BorderLayout layout = new BorderLayout();
setLayout(layout);
setBackground(Color.GRAY);
//
JLabel la0001111 = new JLabel();
java.net.URL imgURLE = ATM.class.getResource("cs.jpg");
la0001111.setIcon(new ImageIcon(imgURLE));
//
Panel ppp = new Panel();
ppp.setLayout(new BorderLayout());
label1.setFont(new Font("Serif", Font.BOLD, 18));
label1.setForeground(Color.CYAN);
ppp.add(label1, BorderLayout.NORTH);
label2.setFont(new Font("Serif", Font.BOLD, 18));
label2.setForeground(Color.CYAN);
ppp.add(label2, BorderLayout.CENTER);
label3.setFont(new Font("Serif", Font.BOLD, 18));
label3.setForeground(Color.CYAN);
ppp.add(label3, BorderLayout.SOUTH);
add(ppp, BorderLayout.NORTH);
add(la0001111, BorderLayout.CENTER);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
Dimension labelSize = frame1.getPreferredSize(); // 获取图形的大小
setLocation(screenSize.width / 2 - (labelSize.width / 2),
screenSize.height / 2 - (labelSize.height / 2)); // 调整界面的位置
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -