2019.03.05更新 好久没有登录CSDN了,今天竟然发现有两位朋友在两月前问我,实在抱歉没有及时回复。由于我的电脑太渣,所以将Oracle 数据库换成了MySQL数据库,之前的源码文件找不到了,于是对下面的程序稍作了修改。现在附上java+mysql的源码文件:
链接:https://pan.baidu.com/s/1-v_PrUB87rshc_O9_fJZ2A
提取码:nszi
==================================以下为原文==========================================
本文的目的是通过自助银行系统掌握数据库编程技术,能正确连接数据库,能使用Java Swing实现程序的界面化,能对数据库中信息进行查询、插入、删除、修改。
1.在Oracle数据库中建立两张表
1-1.建立客户表 t_user,并进行相关约束
create table t_user (
accountnumber varchar2(20) primary key,
username varchar2(16),
password varchar2(6),
balance number(8,2),
datetime date default sysdate
);
1-2.建立交易记录表 t_history
create table t_history (
hid number(10) primary key,
accountnumber varchar2(20),
money number(8,2),
content varchar2(50),
time date default sysdate
);
1-3.建立用户账号序列 seq_account 和交易订单序列 seq_hid
create sequence seq_account
start with 1001
increment by 1;
create sequence seq_hid
start with 1
increment 1;
1-4给t_history 的accountnumber 添加外键约束
alter table t_history add constraints fk_number foreign key(accountnumber) references t_user(accountnumber);
2.Java程序设计
2-1.Java实现数据库的连接,以及完成对开户,取款,存款,转账,查询等功能的定义
package bank.system;
import java.sql.*;
public class BankSystem {
public static Connection connection() throws SQLException, ClassNotFoundException{
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.得到连接
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger1");
return conn;
}
public static String openAccount(String name,String pwd,String pwd1) throws ClassNotFoundException, SQLException{
Connection conn=connection();
//验证两次输入的密码是否相同
if(pwd.equals(pwd1))
{
//创建新用户
String sql1="insert into t_user(accountnumber,username,password,balance) values(seq_account.nextval,?,?,100)";
PreparedStatement psmt=conn.prepareStatement(sql1);
psmt.setString(1, name);
psmt.setString(2, pwd);
ResultSet rs=psmt.executeQuery();
if(rs.next())
{
//返回新用户分配到的账号
Statement sm =conn.createStatement();
ResultSet rs1=sm.executeQuery("select accountnumber from t_user where username='"+name+"'");
while(rs1.next())
{
String str =rs1.getString(1);
return str;
}
}
}
return null;
}
public static String[] balanceInquiry(String num,String psw) throws ClassNotFoundException, SQLException
{
//查询账户信息
Connection conn=connection();
String sql="select * from t_user where accountnumber='"+num+"' and password='"+psw+"'";
PreparedStatement psmt=conn.prepareStatement(sql);
ResultSet rs=psmt.executeQuery();
if(rs.next())
{
String[] str =new String[3];
str[0]=rs.getString(1);//记录账号
str[1]=rs.getString(2);//记录用户名
str[2]=rs.getString(4);//记录余额
return str;
}
return null;
}
public static Boolean deposite(String num,String psw,Float money) throws ClassNotFoundException, SQLException{
Connection conn=connection();
//存款
String sql1="update t_user set balance=balance+? where accountnumber=? and password=?";
PreparedStatement psmt=conn.prepareStatement(sql1);
psmt.setFloat(1, money);
psmt.setString(2, num);
psmt.setString(3, psw);
int rs1=psmt.executeUpdate();
//同时将交易记录存入数据库
String sql2="insert into t_history(hid,accountnumber,money,content) values(seq_hid.nextval,"+num+","+money+",'存款')";
psmt=conn.prepareStatement(sql2);
psmt.executeQuery();
if(rs1>0)
{
return true;
}
else
{
return false;
}
}
public static boolean withdraw(String num,String psw,float money) throws ClassNotFoundException, SQLException{
//取款
Connection conn=connection();
String sql1="update t_user set balance=balance-? where accountnumber=? and password=?";
PreparedStatement psmt=conn.prepareStatement(sql1);
psmt.setFloat(1, money);
psmt.setString(2, num);
psmt.setString(3, psw);
int rs1=psmt.executeUpdate();
//同时将交易记录存入数据库
String sql2="insert into t_history(hid,accountnumber,money,content) values(seq_hid.nextval,"+num+","+money+",'取款')";
psmt=conn.prepareStatement(sql2);
psmt.executeQuery();
if(rs1>0)
{
return true;
}
else
{
return false;
}
}
public static boolean transferAccounts(String fromNum,String psw,String toNum,float money) throws ClassNotFoundException, SQLException
{
//转账
Connection conn=connection();
conn.setAutoCommit(false);//取消自动提交事务
String sql1="update t_user set balance=balance-"+money+" where accountnumber="+fromNum+" and password="+psw+"";
String sql2="update t_user set balance=balance+"+money+" where accountnumber="+toNum+"";
//将交易记录存入数据库
String sql3="insert into t_history(hid,accountnumber,money,content) values(seq_hid.nextval,"+fromNum+","+money+",'转账')";
PreparedStatement pstmt=conn.prepareStatement(sql1);
int result1=pstmt.executeUpdate();
pstmt=conn.prepareStatement(sql2);
int result2=pstmt.executeUpdate();
pstmt=conn.prepareStatement(sql3);
pstmt.executeQuery();
if(result1>0&&result2>0)
{
conn.commit();//提交事务
return true;
}
else
{
conn.rollback();//回滚事务
return false;
}
}
public static boolean accountBalance(String num,float money) throws ClassNotFoundException, SQLException
{//用来判断转账和取款时是否余额不足
Connection conn=connection();
String sql="select balance from t_user where accountnumber=?";
PreparedStatement psmt =conn.prepareStatement(sql);
psmt.setString(1, num);
ResultSet rs = psmt.executeQuery();
if(rs.next())
{
float balance=rs.getFloat(1);
if(money>balance)
return false;
else
return true;
}
return false;
}
}
2-2.开户Swing界面的实现
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;
public class OpenAccount {
private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OpenAccount window = new OpenAccount();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JFrame getFrame()
{
return frame;
}
public OpenAccount() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(SystemColor.control);
frame.setTitle("注册用户");
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setBackground(SystemColor.control);
textPane.setFont(new Font("宋体", Font.PLAIN, 22));
textPane.setText(" 用户名");
textPane.setBounds(45, 42, 93, 42);
frame.getContentPane().add(textPane);
//添加用户名输入文本框
textField = new JTextField();
textField.setBounds(148, 42, 178, 42);
frame.getContentPane().add(textField);
textField.setColumns(10);
//添加密码输入文本框
passwordField = new JPasswordField();
passwordField.setBounds(148, 94, 178, 44);
frame.getContentPane().add(passwordField);
//添加确认密码输入文本框
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(148, 148, 178, 42);
frame.getContentPane().add(passwordField_1);
JTextPane textPane_1 = new JTextPane();
textPane_1.setBackground(SystemColor.control);
textPane_1.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_1.setForeground(new Color(0, 0, 0));
textPane_1.setText(" 密码");
textPane_1.setBounds(55, 94, 85, 44);
frame.getContentPane().add(textPane_1);
JTextPane textPane_2 = new JTextPane();
textPane_2.setBackground(SystemColor.control);
textPane_2.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_2.setText("确认密码");
textPane_2.setBounds(33, 148, 105, 52);
frame.getContentPane().add(textPane_2);
JButton btnNewButton = new JButton("注册");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
//获取密码框中的密码并转换为字符串
char[] password =passwordField.getPassword();
String psw=new String(password);
char[] password1 =passwordField_1.getPassword();
String psw1=new String(password1);
try {
String str=BankSystem.openAccount(name,psw,psw1);
if(str!=null)
{
JOptionPane.showMessageDialog(btnNewButton,"注册成功,您的账号为"+str);
}
else
JOptionPane.showMessageDialog(btnNewButton,"错误,请重新输入!","Error", JOptionPane.ERROR_MESSAGE);
textField.setText("");
passwordField.setText("");
passwordField_1.setText("");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(183, 209, 85, 42);
frame.getContentPane().add(btnNewButton);
}
}
运行结果如下:
2-3 查询Swing界面的实现
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Inquiry {
private JFrame frame;
private JPasswordField passwordField;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Inquiry window = new Inquiry();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JFrame getFrame()
{
return frame;
}
public Inquiry() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("信息查询");
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
//添加密码输入框
passwordField = new JPasswordField();
passwordField.setBounds(144, 136, 204, 43);
frame.getContentPane().add(passwordField);
//添加账号输入文本框
textField = new JTextField();
textField.setBounds(144, 59, 208, 43);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextPane textPane = new JTextPane();
textPane.setBackground(SystemColor.control);
textPane.setFont(new Font("宋体", Font.PLAIN, 20));
textPane.setText(" 账号");
textPane.setBounds(23, 59, 111, 43);
frame.getContentPane().add(textPane);
JTextPane textPane_1 = new JTextPane();
textPane_1.setBackground(SystemColor.control);
textPane_1.setFont(new Font("宋体", Font.PLAIN, 20));
textPane_1.setText(" 密码");
textPane_1.setBounds(59, 136, 82, 43);
frame.getContentPane().add(textPane_1);
JButton btnNewButton = new JButton("确定");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num=textField.getText();
//获取密码框中的密码并转换为字符串
char[] password =passwordField.getPassword();
String psw=new String(password);
try {
String[] str =BankSystem.balanceInquiry(num,psw);
JOptionPane.showMessageDialog(btnNewButton, "账号:"+str[0]+" 用户名:"+str[1]+" 余额:"+str[2]);
} catch (Exception e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(btnNewButton,"账号未注册或密码错误!","Error", JOptionPane.ERROR_MESSAGE);
}
}
});
btnNewButton.setBounds(172, 213, 111, 38);
frame.getContentPane().add(btnNewButton);
}
}
运行结果如下:
2-4 存款Swing界面的实现
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Deposite {
private JFrame frame;
private JTextField textField_1;
private JTextField textField_2;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Deposite window = new Deposite();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JFrame getFrame()
{
return frame;
}
public Deposite() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle(" 存款");
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setBackground(SystemColor.control);
textPane.setFont(new Font("宋体", Font.PLAIN, 22));
textPane.setText(" 账号");
textPane.setBounds(37, 37, 116, 45);
frame.getContentPane().add(textPane);
//添加账号输入文本框
textField_1 = new JTextField();
textField_1.setBounds(176, 37, 186, 45);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JTextPane textPane_1 = new JTextPane();
textPane_1.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_1.setText(" 密码");
textPane_1.setBackground(SystemColor.control);
textPane_1.setBounds(38, 102, 115, 45);
frame.getContentPane().add(textPane_1);
//添加密码输入文本框
passwordField = new JPasswordField();
passwordField.setBounds(176, 102, 186, 45);
frame.getContentPane().add(passwordField);
JTextPane textPane_2 = new JTextPane();
textPane_2.setBackground(SystemColor.control);
textPane_2.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_2.setText(" 存储金额");
textPane_2.setBounds(37, 170, 116, 45);
frame.getContentPane().add(textPane_2);
//添加存款金额文本框
textField_2 = new JTextField();
textField_2.setBounds(178, 170, 184, 45);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
JButton btnNewButton = new JButton("确定");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField_1.getText();
//获取密码框中的密码并转换为字符串
char[] password =passwordField.getPassword();
String psw=new String(password);
try {
float money =Float.parseFloat(textField_2.getText());
try {
boolean b =BankSystem.deposite(num, psw, money);
if(b==true)
{
JOptionPane.showMessageDialog(btnNewButton, "存储成功!");
textField_2.setText("");
passwordField.setText("");
textField_1.setText("");
}
else
{
JOptionPane.showMessageDialog(btnNewButton, "密码错误,请重新输入","Error",JOptionPane.ERROR_MESSAGE);
passwordField.setText("");
}
} catch (ClassNotFoundException | SQLException e2) {
JOptionPane.showMessageDialog(btnNewButton, "您输入账号不存在,请重新输入","Error",JOptionPane.ERROR_MESSAGE);
textField_1.setText("");
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(btnNewButton, "输入的存款金额必须是数字!","Error",JOptionPane.ERROR_MESSAGE);
textField_2.setText("");
}
}
});
btnNewButton.setBounds(131, 225, 115, 36);
frame.getContentPane().add(btnNewButton);
}
}
运行结果如下:
2-5 取款Swing界面的实现
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.SystemColor;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Withdraw {
JFrame frame;
private JTextField textField;
private JTextField textField_2;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Withdraw window = new Withdraw();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JFrame getFrame()
{
return frame;
}
public Withdraw(){
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("取 款");
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setBackground(SystemColor.control);
textPane.setFont(new Font("宋体", Font.PLAIN, 22));
textPane.setText(" 账号");
textPane.setBounds(37, 37, 116, 45);
frame.getContentPane().add(textPane);
//添加账号输入文本框
textField = new JTextField();
textField.setBounds(176, 37, 186, 45);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextPane textPane_1 = new JTextPane();
textPane_1.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_1.setText(" 密码");
textPane_1.setBackground(SystemColor.control);
textPane_1.setBounds(38, 102, 115, 45);
frame.getContentPane().add(textPane_1);
//添加密码输入文本框
passwordField = new JPasswordField();
passwordField.setBounds(176, 102, 186, 45);
frame.getContentPane().add(passwordField);
JTextPane textPane_2 = new JTextPane();
textPane_2.setBackground(SystemColor.control);
textPane_2.setFont(new Font("宋体", Font.PLAIN, 22));
textPane_2.setText(" 取款金额");
textPane_2.setBounds(37, 170, 116, 45);
frame.getContentPane().add(textPane_2);
//添加取款金额输入文本框
textField_2 = new JTextField();
textField_2.setBounds(178, 170, 184, 45);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
JButton btnNewButton = new JButton("确定");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String num = textField.getText();
//获取密码框中的密码并转换为字符串
char[] password =passwordField.getPassword();
String psw=new String(password);
try {
float money =Float.parseFloat(textField_2.getText());
boolean bl=BankSystem.accountBalance(num,money);
if(bl==false)
{
JOptionPane.showMessageDialog(btnNewButton,"您的余额不足或账户不存在");
textField_2.setText("");
}
else
{
boolean b =BankSystem.withdraw(num,psw,money);
if(b==true)
{
JOptionPane.showMessageDialog(btnNewButton, "取款成功,请收好您的现金!");
textField_2.setText("");
passwordField.setText("");
textField.setText("");
}
else
{
JOptionPane.showMessageDialog(btnNewButton, "密码错误,请重新输入","Error",JOptionPane.ERROR_MESSAGE);
passwordField.setText("");
}
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(btnNewButton, "输入的取款金额必须是数字!","Error",JOptionPane.ERROR_MESSAGE);
textField_2.setText("");
}
}
});
btnNewButton.setBounds(131, 225, 115, 36);
frame.getContentPane().add(btnNewButton);
}
}
运行结果如下:
2-6 转账Swing界面的实现
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TransferAccounts {
private JFrame frame;
private JTextField textField;
private JTextField textField_2;
private JPasswordField passwordField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TransferAccounts window = new TransferAccounts();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TransferAccounts() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public JFrame getFrame()
{
return frame;
}
private void initialize() {
frame = new JFrame();
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextPane textPane = new JTextPane();
textPane.setFont(new Font("宋体", Font.PLAIN, 20));
textPane.setBackground(SystemColor.control);
textPane.setText(" 账号");
textPane.setBounds(40, 29, 103, 32);
frame.getContentPane().add(textPane);
//添加账号输入文本框
textField = new JTextField();
textField.setBounds(153, 29, 176, 32);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextPane textPane_1 = new JTextPane();
textPane_1.setFont(new Font("宋体", Font.PLAIN, 20));
textPane_1.setBackground(SystemColor.control);
textPane_1.setText(" 密码");
textPane_1.setBounds(40, 71, 103, 40);
frame.getContentPane().add(textPane_1);
JTextPane textPane_2 = new JTextPane();
textPane_2.setBackground(SystemColor.control);
textPane_2.setFont(new Font("宋体", Font.PLAIN, 20));
textPane_2.setText(" 对方账号");
textPane_2.setBounds(40, 121, 103, 40);
frame.getContentPane().add(textPane_2);
//添加对方账号输入文本框
textField_2 = new JTextField();
textField_2.setBounds(153, 121, 176, 32);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
//添加密码输入文本框
passwordField = new JPasswordField();
passwordField.setBounds(153, 71, 176, 32);
frame.getContentPane().add(passwordField);
JTextPane textPane_3 = new JTextPane();
textPane_3.setFont(new Font("宋体", Font.PLAIN, 20));
textPane_3.setBackground(SystemColor.control);
textPane_3.setText(" 转账金额");
textPane_3.setBounds(40, 170, 103, 40);
frame.getContentPane().add(textPane_3);
//添加转账金额输入文本框
textField_1 = new JTextField();
textField_1.setBounds(155, 170, 174, 32);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("确 定");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fromNum = textField.getText();
//获取密码框中的密码并转换为字符串
char[] password =passwordField.getPassword();
String psw=new String(password);
String toNum = textField_2.getText();
String str_money =textField_1.getText();
if(fromNum.equals(toNum))
{
JOptionPane.showMessageDialog(btnNewButton, "您不能给自己转账!","Error",JOptionPane.ERROR_MESSAGE);
textField_2.setText("");
}
else
{
try {
float money=Float.parseFloat(str_money);
boolean bl=BankSystem.accountBalance(fromNum, money);
if(bl==false)
{
JOptionPane.showMessageDialog(btnNewButton,"您的余额不足或账户不存在");
textField_1.setText("");
}
else
{
boolean b = BankSystem.transferAccounts(fromNum, psw, toNum, money);
if(b==true)
{
JOptionPane.showMessageDialog(btnNewButton,"转账成功");
//textField.setText("");
textField_1.setText("");
textField_2.setText("");
//passwordField.setText("");
}
else
{
JOptionPane.showMessageDialog(btnNewButton, "密码错误或对方账号不存在,请重新输入","Error",JOptionPane.ERROR_MESSAGE);
passwordField.setText("");
textField_2.setText("");
}
}
}catch(Exception e1)
{
e1.printStackTrace();
JOptionPane.showMessageDialog(btnNewButton, "输入的转账金额必须是数字!","Error",JOptionPane.ERROR_MESSAGE);
textField_1.setText("");
}
}
}
});
btnNewButton.setBounds(135, 220, 125, 31);
frame.getContentPane().add(btnNewButton);
}
}
运行结果如下
2-7 自助银行系统主界面的实现
将每一个分界面都加入到主界面按钮的监视器中。
package bank.system;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
public class BankWindow {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BankWindow window = new BankWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BankWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("自助银行系统");
frame.setForeground(Color.DARK_GRAY);
frame.getContentPane().setFont(new Font("微软雅黑", Font.BOLD, 16));
frame.getContentPane().setForeground(new Color(0, 0, 0));
frame.getContentPane().setBackground(SystemColor.control);
frame.setFont(null);
frame.setBackground(Color.GRAY);
frame.setBounds(400, 200, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnNewButton = new JButton("开 户");
btnNewButton.setBounds(10, 83, 175, 33);
btnNewButton.setBackground(SystemColor.control);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
OpenAccount window = new OpenAccount();
window.getFrame().setVisible(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
frame.getContentPane().setLayout(null);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("存 款");
btnNewButton_1.setBounds(10, 155, 175, 33);
btnNewButton_1.setForeground(new Color(0, 0, 0));
btnNewButton_1.setBackground(SystemColor.control);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Deposite window = new Deposite();
window.getFrame().setVisible(true);
}
});
frame.getContentPane().add(btnNewButton_1);
JButton button = new JButton("取 款");
button.setBounds(10, 220, 175, 33);
button.setBackground(SystemColor.control);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Withdraw window = new Withdraw();
window.getFrame().setVisible(true);
}
});
frame.getContentPane().add(button);
JButton btnNewButton_2 = new JButton("转 账");
btnNewButton_2.setBounds(249, 157, 175, 29);
btnNewButton_2.setBackground(SystemColor.control);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TransferAccounts window =new TransferAccounts();
window.getFrame().setVisible(true);
}
});
frame.getContentPane().add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("退 出");
btnNewButton_3.setBounds(249, 222, 178, 29);
btnNewButton_3.setBackground(SystemColor.control);
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(btnNewButton_3, "期待您下次使用,再见");
System.exit(0);
}
});
frame.getContentPane().add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("查 询");
btnNewButton_4.setBounds(249, 83, 175, 33);
btnNewButton_4.setBackground(SystemColor.control);
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Inquiry window = new Inquiry();
window.getFrame().setVisible(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
frame.getContentPane().add(btnNewButton_4);
JTextPane textPane = new JTextPane();
textPane.setBounds(80, 20, 298, 33);
frame.getContentPane().add(textPane);
textPane.setFont(new Font("宋体", Font.BOLD, 14));
textPane.setBackground(SystemColor.control);
textPane.setText("欢迎使用自助银行系统,请选择所需服务");
JTextPane textPane_1 = new JTextPane();
textPane_1.setForeground(new Color(0, 0, 0));
textPane_1.setBackground(SystemColor.control);
textPane_1.setText(" 请注意周边环境安全");
textPane_1.setBounds(143, 52, 150, 21);
frame.getContentPane().add(textPane_1);
}
}
运行结果如下:
3.小结:目前自助银行系统客户端程序设计告一段落了,喜欢的朋友们可以自行添加修改其他功能,也可以自己做一个管理员的界面窗口。
程序设计中使用了swing界面设计的工具windowbuilder,感兴趣的朋友可以访问:https://blog.csdn.net/qq_28859405/article/details/52562131
另外还有Oracle 11g驱动的jar包
http://www.onlinedown.net/soft/1166170.htm
https://blog.csdn.net/qiannianguji01/article/details/50506158
欢迎大家对本程序中的不足提出宝贵的建议和意见。