📄 tranferpanel.java
字号:
package bank;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.awt.Color;
//转帐界面........
public class TranferPanel extends JPanel{
public TranferPanel(String accountID,String branchname,String pswd) {
try {
jbInit();
this.accountID=accountID;
this.branchname=branchname;
this.pswd=pswd;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
jButton3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jTextField2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
jLabel1.setText("输入转帐对方的卡号:");
this.setLayout(xYLayout1);
jLabel2.setText("卡号类型为:");
jButton1.setText("确定");
jButton2.setVisible(false);
jButton1.addActionListener(new TranferPanel_jButton1_actionAdapter(this));
jTextField2.setEditable(false);
jTextField2.setText("等待中...");
jButton2.setText("确定转帐");
jButton2.addActionListener(new TranferPanel_jButton2_actionAdapter(this));
jButton3.setText("重置");
jButton3.addActionListener(new TranferPanel_jButton3_actionAdapter(this));
jLabel3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
jLabel3.setText("转帐金额:");
jLabel4.setForeground(Color.red);
jLabel4.setText("--------------------------------------------------------------------------------------------");
this.setBackground(Color.pink);
xYLayout1.setWidth(416);
xYLayout1.setHeight(302);
jButton4.setText("打印");
jButton4.addActionListener(new TranferPanel_jButton4_actionAdapter(this));
jButton4.setVisible(false);
this.add(jTextField1, new XYConstraints(49, 60, 247, 30));
this.add(jLabel1, new XYConstraints(21, 17, 173, 44));
this.add(jLabel1, new XYConstraints(21, 17, 183, 44));
this.add(jButton1, new XYConstraints(326, 64, 76, 28));
this.add(jButton3, new XYConstraints(326, 108, 78, 28));
this.add(jLabel2, new XYConstraints(15, 164, 87, 34));
this.add(jLabel4, new XYConstraints(9, 144, 391, 18));
this.add(jLabel3, new XYConstraints(17, 102, 74, 32));
this.add(jTextField3, new XYConstraints(90, 108, 206, 32));
this.add(jTextField2, new XYConstraints(103, 160, 229, 34));
this.add(jTextArea1, new XYConstraints(19, 207, 291, 86));
this.add(jButton2, new XYConstraints(324, 200, 87, 31));
this.add(jButton4, new XYConstraints(331, 248, 71, 29));
}
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JTextField jTextField2 = new JTextField();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
private Statement stmt;
private Connection con;
JTextField jTextField3 = new JTextField();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
private String accountID;
private String branchname;
private String pswd;
static String bran;
static float mon=0;
static float getm=0;
static String toname;
JTextArea jTextArea1 = new JTextArea();
JButton jButton4 = new JButton();
public void jButton3_actionPerformed(ActionEvent e) {
jTextField1.setText("");
jTextField2.setText("等待中...");
jTextField3.setText("");
jTextArea1.setText("");
jButton1.setVisible(true);
jButton2.setVisible(false);
jButton4.setVisible(false);
}
int time=0;
String card="";
public void jButton1_actionPerformed(ActionEvent e) {
if(jTextField1.getText().trim().equals(""))
{
jTextField1.setText("");
JOptionPane.showMessageDialog(null, "输入错误,请输入要转的帐号!");
return;
}
if(jTextField1.getText().trim().equals(accountID))
{
jTextField1.setText("");
JOptionPane.showMessageDialog(null, "不能进行同卡转帐!");
return;
}
time++;//计算输入的次数
if(time<2){
toname = jTextField1.getText().trim();
jTextField1.setText("");
JOptionPane.showMessageDialog(null, "请再输入要转的帐号!");
return;
}
card = jTextField1.getText().trim();
if(!card.equals(toname))
{
time=0;
jTextField1.setText("");
JOptionPane.showMessageDialog(null, "要转的帐号不一致!");
JOptionPane.showMessageDialog(null, "请重新输入要转的帐号!");
return;
}
try{
con = bankconnect.getconn();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"select branch_name from account where account_num like '%" + toname +
"%'");
while (rs.next()) {
bran = rs.getString("branch_name");
jTextField2.setText(bran);
String sql = "select balance from account where account_num like '%" + accountID +
"%'";
rs=stmt.executeQuery(sql);
String a="";
while (rs.next()) a= rs.getString("balance");
mon=Float.parseFloat(a);
jButton1.setVisible(false);
jButton2.setVisible(true);
return;
}
JOptionPane.showMessageDialog(null, "the customer is not exist!");
}
catch(SQLException ex){
System.out.print("数据库连接出错!"+ex.toString());
}
}
public void jButton2_actionPerformed(ActionEvent e) throws SQLException {
try {
String sql;
con = bankconnect.getconn();
Statement stmt = con.createStatement();
System.out.println(jTextField3.getText().trim().equals(""));
if(!jTextField3.getText().trim().equals("")){
if(Integer.parseInt(jTextField3.getText().trim())<0)
{
jTextField3.setText("");
JOptionPane.showMessageDialog(null, "转帐的数目不能少于0,请重新输入金额!");
}
getm = Float.parseFloat(jTextField3.getText().trim());
sql = "select balance from account where account_num like '%" + accountID +
"%'";
ResultSet rs=stmt.executeQuery(sql);
String a="";
while (rs.next()) a= rs.getString("balance");
mon=Float.parseFloat(a);
if (bran.equals(branchname)) { //同行转帐
if ( (mon - (getm * 0.02+getm)) >= 0) {
try{
//设置事务,
con.setAutoCommit(false);
sql = "update account set balance=balance-" + (getm * 0.02+getm) +
"where account_num like '%" + accountID + "%'";
stmt.executeUpdate(sql);
sql = "update account set balance=balance+" + getm +
"where account_num like '%" + toname + "%'";
stmt.executeUpdate(sql);
jTextArea1.append("今天是:"+TodayTime.getLongDate()+",你已经成功转帐"+getm+"RMB,谢谢光临!");
sql = "insert into message(account_num,message_context,message_time,message_class) values('" +
accountID + "','" + jTextArea1.getText() + "','" +
TodayTime.getLongDate() + "',3)";
//打印信息。。。
stmt.executeUpdate(sql);
con.commit();
con.setAutoCommit(true);
jButton1.setVisible(true);
jButton2.setVisible(false);
jButton4.setVisible(true);
}
catch(Exception exp){
con.rollback();//回滚
exp.printStackTrace();
stmt.close();
con.close();
}
}
else {
jTextArea1.append("余额不足,谢谢光临!");
}
}
else { //异行转帐
if ( (mon - (getm * 0.05+getm)) >= 0) {
//设置事务,回滚
try{
con.setAutoCommit(false);// 更改JDBC事务的默认提交方式
sql = "update account set balance=balance-" + (getm * 0.05+getm) +
"where account_num like '%" + accountID + "%'";
stmt.executeUpdate(sql);
sql = "update account set balance=balance+" + getm +
"where account_num like '%" + toname + "%'";
stmt.executeUpdate(sql);
jTextArea1.append("今天是:"+TodayTime.getLongDate()+",你已经成功转帐"+getm+"RMB,谢谢光临!");
sql = "insert into message(account_num,message_context,message_time,message_class) values('" +
accountID + "','" + jTextArea1.getText() + "','" +
TodayTime.getLongDate() + "',3)";
//打印信息。。。
stmt.executeUpdate(sql);
con.commit();//提交JDBC事务
con.setAutoCommit(true);// 恢复JDBC事务的默认提交方式
jButton1.setVisible(true);
jButton2.setVisible(false);
jTextArea1.append("你已经成功转帐,谢谢光临!");
jButton4.setVisible(true);
}
catch(Exception exp){
con.rollback();
stmt.close();
exp.printStackTrace();
con.close();
}
}
else {
jTextArea1.append("余额不足,谢谢光临!");
}
}
}
else{
JOptionPane.showMessageDialog(null, "请输入金额");
}
}
catch(SQLException ex){
System.out.print("数据库连接出错!"+ex.toString());
}
}
public void jButton4_actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"操作成功!!!");
jTextField1.setText("");
jTextField2.setText("等待中...");
jTextField3.setText("");
jTextArea1.setText("");
jButton1.setVisible(true);
jButton2.setVisible(false);
jButton4.setVisible(false);
}
}
class TranferPanel_jButton4_actionAdapter
implements ActionListener {
private TranferPanel adaptee;
TranferPanel_jButton4_actionAdapter(TranferPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
class TranferPanel_jButton2_actionAdapter
implements ActionListener {
private TranferPanel adaptee;
TranferPanel_jButton2_actionAdapter(TranferPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e){
try {
adaptee.jButton2_actionPerformed(e);
}
catch (SQLException ex) {
}
}
}
class TranferPanel_jButton1_actionAdapter
implements ActionListener {
private TranferPanel adaptee;
TranferPanel_jButton1_actionAdapter(TranferPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class TranferPanel_jButton3_actionAdapter
implements ActionListener {
private TranferPanel adaptee;
TranferPanel_jButton3_actionAdapter(TranferPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -