Java swing圖書管理系統(tǒng)5(個人設(shè)計思考得出的全功能簡易原版)


package denglu;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class tuShuLeiXingGuanLi extends JFrame {
class shijian_GULtushutype implements MouseListener, ActionListener {
public tuShuLeiXingGuanLi zz = null;
public shijian_GULtushutype(tuShuLeiXingGuanLi z) {
this.zz = z;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String str = arg0.getActionCommand();
if (str.equals("查詢類型")) {
String tname = zz.txt_ttypename.getText();
// String sql="select * from typetushu where ttypename like ' %"
// +tname+"%'";
zz.typeload(tname);
} else if (str.equals("修改類型")) {
String tid = zz.txt_id.getText();
String tname = zz.txt_ttypename.getText();
String sql = "update ts_type set ts_leixing='" + tname
+ "' where ts_leixingid='" + tid + "'";
if (DBUtils.ZSG(sql)) {
zz.typeload(null);
JOptionPane.showMessageDialog(null, "修改成功");
} else {
JOptionPane.showMessageDialog(null, "修改失敗,請重試");
}
} else if (str.equals("增加類型")) {
String tid = zz.txt_id.getText();
String tname = zz.txt_ttypename.getText();
String sql = "insert into ts_type values('" + tname + "')";
if (DBUtils.ZSG(sql)) {
zz.typeload(null);
JOptionPane.showMessageDialog(null, "增加成功");
} else {
JOptionPane.showMessageDialog(null, "增加失敗,請重試");
}
} else if (str.equals("清空")) {
zz.txt_id.setText("");
zz.txt_ttypename.setText("");
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// T0O Auto-generated method stub
if (arg0.getClickCount() == 2) {
// 雙擊事件
int row = zz.tb1.getSelectedRow();
zz.txt_id.setText(zz.tb1.getValueAt(row, 0).toString());
zz.txt_ttypename.setText(zz.tb1.getValueAt(row, 1).toString());
} else if (arg0.isMetaDown()) {
// 右擊事件
int row = zz.tb1.getSelectedRow();
String id = zz.tb1.getValueAt(row, 0).toString();
String sql = "delete ts_type where ts_leixingid='" + id + "'";
int num = JOptionPane.showConfirmDialog(null, "確定要冊除這條信息嗎?");
if (num == 0) {
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "冊除成功");
zz.typeload(null);
return;
// 刪除成功再查詢一遍表格就可以實現(xiàn)表格的刷新,但是我們的查詢寫在了構(gòu)造方法里,內(nèi)容很多,
// 這里想要使用又要在寫一遍,很麻煩,那么現(xiàn)在我們寫一個方法 將查詢內(nèi)容封裝進去,再想查詢的
// 時候只要調(diào)用查詢的方法即可
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯誤,請重試");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public static JButton btn1, btn2, btn3, btn4 = null;
public static DefaultTableModel dtm = null;
public static JPanel jp1, jp2 = null;
public static JLabel lb_id, lb_name = null;
public static JTable tb1 = null;
public static JTextField txt_id, txt_ttypename = null;
public tuShuLeiXingGuanLi() {
this.setTitle("圖書類型管理");
this.setLayout(null);
this.setSize(600, 600);
this.setLocationRelativeTo(null);
jp2 = new JPanel();
jp2.setLayout(null);
jp2.setBounds(368, 10, 200, 350);
jp2.setBorder(BorderFactory.createLineBorder(Color.gray));
lb_id = new JLabel("類型id:");
lb_name = new JLabel("類型名稱:");
lb_id.setBounds(10, 20, 70, 30);
lb_name.setBounds(10, 60, 70, 30);
jp2.add(lb_id);
jp2.add(lb_name);
txt_id = new JTextField();
txt_id.setEditable(false);
txt_ttypename = new JTextField();
txt_id.setBounds(65, 20, 120, 30);
txt_ttypename.setBounds(65, 60, 120, 30);
jp2.add(txt_id);
jp2.add(txt_ttypename);
btn1 = new JButton("查詢類型");
btn2 = new JButton("修改類型");
btn3 = new JButton("增加類型");
btn4 = new JButton("清空");
btn1.setBounds(0, 100, 100, 30);
btn2.setBounds(110, 100, 100, 30);
btn3.setBounds(0, 140, 100, 30);
btn4.setBounds(110, 140, 100, 30);
btn1.addActionListener(new shijian_GULtushutype(this));
btn2.addActionListener(new shijian_GULtushutype(this));
btn3.addActionListener(new shijian_GULtushutype(this));
btn4.addActionListener(new shijian_GULtushutype(this));
jp2.add(btn1);
jp2.add(btn2);
jp2.add(btn3);
jp2.add(btn4);
this.add(jp2);
this.setVisible(true);
typeload(null);
}
public tuShuLeiXingGuanLi(Object object) {
// TODO Auto-generated constructor stub
}
// 先布局左側(cè)內(nèi)容, 數(shù)據(jù)庫查詢,提取出-一個查詢的方法
public void typeload(String str) {
if (jp1 != null) {
this.remove(jp1);
}
String sql = "select * from ts_type where 1=1";
if (str != null) {
sql += "and ts_leixing like '%" + str + "%'";
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("類型編號");
v_head.add("類型名稱");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
ResultSet res = DBUtils.Select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("ts_leixingid"));
v.add(res.getString("ts_leixing"));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dtm = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
// dtm = new DefaultTableModel(v_body, v_head);
tb1 = new JTable(dtm);
tb1.addMouseListener(new shijian_GULtushutype(this));
JScrollPane jsp = new JScrollPane(tb1);
jsp.setBounds(0, 0, 350, 400);
jp1 = new JPanel();
jp1.setLayout(null);
jp1.add(jsp);
jp1.setBounds(8, 0, 350, 400);
// jp1.addMouseListener(new shijian_GULtushutype(this));
this.add(jp1);
}
}


package denglu;
public class yonghu {
private String yh_A;
private int yh_id;
private String yh_name;
private String yh_phone;
private String yh_pwd;
private String yh_Q;
private String yh_sex;
private String yh_typename;
private String yh_uname;
public yonghu() {
}
public yonghu(String yh_Q, int yh_id, String yh_uname, String yh_name,
String yh_phone, String yh_pwd, String yh_sex, String yh_typename,
String yh_A) {
// super();
this.yh_Q = yh_Q;
this.yh_id = yh_id;
this.yh_uname = yh_uname;
this.yh_name = yh_name;
this.yh_phone = yh_phone;
this.yh_pwd = yh_pwd;
this.yh_sex = yh_sex;
this.yh_typename = yh_typename;
this.yh_A = yh_A;
}
public String getYh_A() {
return yh_A;
}
public int getYh_id() {
return yh_id;
}
public String getYh_name() {
return yh_name;
}
public String getYh_phone() {
return yh_phone;
}
public String getYh_pwd() {
return yh_pwd;
}
public String getYh_Q() {
return yh_Q;
}
public String getYh_sex() {
return yh_sex;
}
public String getYh_typename() {
return yh_typename;
}
public String getYh_uname() {
return yh_uname;
}
public void setYh_A(String yh_A) {
this.yh_A = yh_A;
}
public void setYh_id(int yh_id) {
this.yh_id = yh_id;
}
public void setYh_name(String yh_name) {
this.yh_name = yh_name;
}
public void setYh_phone(String yh_phone) {
this.yh_phone = yh_phone;
}
public void setYh_pwd(String yh_pwd) {
this.yh_pwd = yh_pwd;
}
public void setYh_Q(String yh_Q) {
this.yh_Q = yh_Q;
}
public void setYh_sex(String yh_sex) {
this.yh_sex = yh_sex;
}
public void setYh_typename(String yh_typename) {
this.yh_typename = yh_typename;
}
public void setYh_uname(String yh_uname) {
this.yh_uname = yh_uname;
}
@Override
public String toString() {
return "users [yh_Q=" + yh_Q + ", yh_id=" + yh_id + ", yh_uname="
+ yh_uname + ", yh_name=" + yh_name + ", yh_phone=" + yh_phone
+ ", yh_pwd=" + yh_pwd + ", yh_sex=" + yh_sex
+ ", yh_typename=" + yh_typename + ", yh_A=" + yh_A + "]";
}
}


package denglu;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import Model.DBUlits;
class shijian_GUL_users1 implements MouseListener, ActionListener {
public yonghuguanli zz = null;
public shijian_GUL_users1(yonghuguanli z) {
this.zz = z;
}
// 按鈕的方法
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String btn_wenzi = arg0.getActionCommand();
if (btn_wenzi.equals("查詢")) {
// 先獲取條件
String uid = zz.txt_id.getText().trim();
String uname = zz.txt_uname.getText().trim();
System.out.println(uid + "? " + uname);
zz.users_Load(uid, uname);
} else if (btn_wenzi.equals("增加用戶")) {
String uname = zz.txt_uname1.getText().trim();
String pwd = zz.txt_upwd.getText().trim();
String unames = zz.txt_unames.getText().trim();
String phone = zz.txt_uphone.getText().trim();
String sex = "男";
if (zz.rb2.isSelected()) {
sex = "女";
}
String uwen = zz.txt_uwen.getText().trim();
String uda = zz.txt_uda.getText().trim();
String sql = "insert into yonghu(yh_uname,yh_pwd,yh_type,yh_name,yh_phone,yh_sex,yh_Q,yh_A) values('"
+ uname
+ "','"
+ pwd
+ "','普通用戶','"
+ unames
+ "','"
+ phone + "','" + sex + "','" + uwen + "','" + uda + "')";
if (DBUlits.zsg(sql)) {
JOptionPane.showMessageDialog(null, "增加成功!");
zz.users_Load(null, null);
} else {
JOptionPane.showMessageDialog(null, "增加失敗,請重試!");
}
} else if (btn_wenzi.equals("修改用戶")) {
String uid = zz.txt_uid.getText().trim();
String uname = zz.txt_uname1.getText().trim();
String pwd = zz.txt_upwd.getText().trim();
String unames = zz.txt_unames.getText().trim();
String phone = zz.txt_uphone.getText().trim();
String sex = "男";
if (zz.rb2.isSelected()) {
sex = "女";
}
String uwen = zz.txt_uwen.getText().trim();
String uda = zz.txt_uda.getText().trim();
// String
// sql="insert into users values('"+uname+"','"+pwd+"','普通用戶','"+unames+"','"+phone+"','"+sex+"','"+uwen+"','"+uda+"')";
String sql = "update yonghu set yh_uname='" + uname + "',yh_pwd='"
+ pwd + "',yh_name='" + unames + "',yh_phone='" + phone
+ "',yh_sex='" + sex + "',yh_Q='" + uwen + "',yh_A='" + uda
+ "' where yh_id='" + uid + "'";
if (DBUlits.zsg(sql)) {
JOptionPane.showMessageDialog(null, "修改成功!");
zz.users_Load(null, null);
} else {
JOptionPane.showMessageDialog(null, "修改失敗,請重試!");
}
} else if (btn_wenzi.equals("清空")) {
zz.txt_uid.setText("");
zz.txt_uname1.setText("");
zz.txt_upwd.setText("");
// zz.txt_utypename.setText("");
zz.txt_uphone.setText("");
zz.txt_unames.setText("");
zz.txt_uwen.setText("");
zz.txt_uda.setText("");
zz.rb1.isSelected();
}
}
// 鼠標的方法
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getClickCount() == 2) {
int row = zz.jt1.getSelectedRow();
zz.txt_uid.setText(zz.jt1.getValueAt(row, 0).toString());
zz.txt_uname1.setText(zz.jt1.getValueAt(row, 1).toString());
zz.txt_upwd.setText(zz.jt1.getValueAt(row, 2).toString());
String type = zz.jt1.getValueAt(row, 3).toString();
zz.com_type.setSelectedItem(type);
zz.txt_unames.setText(zz.jt1.getValueAt(row, 4).toString());
zz.txt_uphone.setText(zz.jt1.getValueAt(row, 5).toString());
String sex = zz.jt1.getValueAt(row, 6).toString();
// 判斷這個內(nèi)容,給相應(yīng)的控件設(shè)置
if (sex.equals("女")) {
zz.rb2.setSelected(true);
} else if (sex.equals("男")) {
zz.rb1.setSelected(true);
}
zz.txt_uwen.setText(zz.jt1.getValueAt(row, 7).toString());
zz.txt_uda.setText(zz.jt1.getValueAt(row, 8).toString());
} else if (arg0.isMetaDown()) {
// 如果是右鍵點擊
int num = JOptionPane.showConfirmDialog(null, "是否確認刪除?");
if (num == 0) {
int row = zz.jt1.getSelectedRow();
String uid = zz.jt1.getValueAt(row, 0).toString();
String sql = "delete yonghu where yh_id='" + uid + "'";
if (DBUlits.zsg(sql)) {
JOptionPane.showMessageDialog(null, "用戶刪除成功!");
zz.users_Load(null, null);
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯誤,請重試!");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public class yonghuguanli extends JFrame {
public static ArrayList<Integer> arr_type = null;
public static JButton btn_insert, btn_update, btn_qingkong = null;
public static JButton btn_select = null;
public static JComboBox com_type = null;
// 1.先做表格的查詢內(nèi)容
public static DefaultTableModel dtm = null;
public static JPanel jp1, jp2, jp3 = null;
public static JTable jt1 = null;
public static JLabel lb_id, lb_uname = null;
public static JLabel lb_uid, lb_uname1, lb_upwd, lb_utypename, lb_unames,
lb_uphone, lb_usex, lb_uwen, lb_uda = null;
public static JRadioButton rb1, rb2 = null;
public static JTextField txt_id, txt_uname = null;
public static JTextField txt_uid, txt_uname1, txt_upwd, txt_utypename,
txt_unames, txt_uphone, txt_uwen, txt_uda = null;
public yonghuguanli() {
this.setLayout(null);
this.setTitle("用戶管理");
this.setSize(850, 650);
this.setLocationRelativeTo(null);
jp2 = new JPanel();
jp2.setBorder(BorderFactory.createLineBorder(Color.gray));
jp2.setBounds(510, 0, 220, 130);
jp2.setLayout(null);
com_type = new JComboBox();
String sql = "select * from yh_type";
ResultSet res_type = DBUlits.select(sql);
arr_type = new ArrayList<Integer>();
try {
while (res_type.next()) {
com_type.addItem(res_type.getObject(2));
arr_type.add(res_type.getInt(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
com_type.setBounds(590, 270, 120, 30);
this.add(com_type);
lb_id = new JLabel("用戶編號:");
lb_id.setBounds(10, 10, 70, 30);
lb_uname = new JLabel("用戶名:");
lb_uname.setBounds(10, 50, 70, 30);
txt_id = new JTextField();
txt_uname = new JTextField();
txt_id.setBounds(80, 10, 120, 30);
txt_uname.setBounds(80, 50, 120, 30);
btn_select = new JButton("查詢");
btn_select.setBounds(30, 90, 130, 30);
jp2.add(btn_select);
jp2.add(lb_id);
jp2.add(lb_uname);
jp2.add(txt_id);
jp2.add(txt_uname);
jp3 = new JPanel();
jp3.setBorder(BorderFactory.createLineBorder(Color.gray));
jp3.setBounds(510, 140, 220, 460);
jp3.setLayout(null);
lb_uid = new JLabel("用戶編號:");
lb_uname1 = new JLabel("用戶名:");
lb_upwd = new JLabel("密碼:");
lb_utypename = new JLabel("用戶類型:");
lb_unames = new JLabel("真實姓名:");
lb_uphone = new JLabel("手機號:");
lb_usex = new JLabel("性別:");
lb_uwen = new JLabel("問題:");
lb_uda = new JLabel("答案:");
lb_uid.setBounds(10, 10, 70, 30);
lb_uname1.setBounds(10, 50, 70, 30);
lb_upwd.setBounds(10, 90, 70, 30);
lb_utypename.setBounds(10, 130, 70, 30);
lb_unames.setBounds(10, 170, 70, 30);
lb_uphone.setBounds(10, 210, 70, 30);
lb_usex.setBounds(10, 250, 70, 30);
lb_uwen.setBounds(10, 290, 70, 30);
lb_uda.setBounds(10, 330, 70, 30);
txt_uid = new JTextField();
txt_uname1 = new JTextField();
txt_uid.setEditable(false);// 編號id不可改,所以用上false等
txt_upwd = new JTextField();
// txt_utypename = new JTextField();
txt_uphone = new JTextField();
txt_unames = new JTextField();
txt_uwen = new JTextField();
txt_uda = new JTextField();
txt_uid.setBounds(80, 10, 120, 30);
txt_uname1.setBounds(80, 50, 120, 30);
txt_upwd.setBounds(80, 90, 120, 30);
// txt_utypename.setBounds(80, 130, 120, 30);
txt_unames.setBounds(80, 170, 120, 30);
txt_uphone.setBounds(80, 210, 120, 30);
txt_uwen.setBounds(80, 290, 120, 30);
txt_uda.setBounds(80, 330, 120, 30);
rb1 = new JRadioButton("男", true);
rb2 = new JRadioButton("女");
rb1.setBounds(80, 250, 60, 30);
rb2.setBounds(140, 250, 60, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
txt_uid.setEnabled(false);
btn_insert = new JButton("增加用戶");
btn_update = new JButton("修改用戶");
btn_qingkong = new JButton("清空");
btn_insert.setBounds(10, 400, 100, 20);
btn_update.setBounds(110, 400, 100, 20);
btn_qingkong.setBounds(10, 430, 170, 20);
jp3.add(btn_insert);
jp3.add(btn_update);
jp3.add(txt_uid);
jp3.add(txt_uname1);
jp3.add(txt_upwd);
// jp3.add(txt_utypename);
jp3.add(txt_unames);
jp3.add(btn_qingkong);
jp3.add(txt_uphone);
jp3.add(txt_uwen);
jp3.add(txt_uda);
jp3.add(rb1);
jp3.add(rb2);
jp3.add(lb_uid);
jp3.add(lb_uname1);
jp3.add(lb_upwd);
jp3.add(lb_utypename);
jp3.add(lb_unames);
jp3.add(lb_uphone);
jp3.add(lb_usex);
jp3.add(lb_uwen);
jp3.add(lb_uda);
this.add(jp2);
this.add(jp3);
btn_select.addActionListener(new shijian_GUL_users1(this));
btn_insert.addActionListener(new shijian_GUL_users1(this));
btn_update.addActionListener(new shijian_GUL_users1(this));
btn_qingkong.addActionListener(new shijian_GUL_users1(this));
this.setVisible(true);
// 查詢表格的內(nèi)容的方法
users_Load(null, null);
}
// 構(gòu)建一個查詢的方法
public void users_Load(String uid, String username) {
// 為了用于頁面刷新做判斷
if (jp1 != null) {
this.remove(jp1);
}
String sql = "select * from yonghu where 1=1";
if (uid != null) {
if (uid.length() > 0) {
sql += " and yh_id='" + uid + "'";
}
}
if (username != null) {
if (username.length() > 0) {
sql += " and yh_name like '%" + username + "%'";
}
}
System.out.println(sql);
Vector<Object> v_head = new Vector<Object>();
v_head.add("用戶編號");
v_head.add("用戶名");
v_head.add("密碼");
v_head.add("用戶類型");
v_head.add("真實姓名");
v_head.add("手機號");
v_head.add("性別");
v_head.add("問題");
v_head.add("答案");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
// 這個存儲的是內(nèi)容
ResultSet res = DBUlits.select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getObject(1));
v.add(res.getObject(2));
v.add(res.getObject(3));
v.add(res.getObject(12));
v.add(res.getObject(4));
v.add(res.getObject(7));
v.add(res.getObject(6));
v.add(res.getObject(10));
v.add(res.getObject(11));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 填充表格
dtm = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
jt1 = new JTable(dtm);
jt1.addMouseListener(new shijian_GUL_users1(this));
JScrollPane jsp = new JScrollPane(jt1);
jsp.setBounds(0, 0, 500, 600);
jp1 = new JPanel();
jp1.add(jsp);
jp1.setLayout(null);
jp1.setBounds(0, 0, 500, 600);
this.add(jp1);
}
}

package denglu;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class shijian_zhmm implements FocusListener, ActionListener {
public zhaoHuiMiMa shijian_zhaoHuiMiMa1 = null;
public shijian_zhmm(zhaoHuiMiMa shijian_zhaoHuiMiMa2) {
this.shijian_zhaoHuiMiMa1 = shijian_zhaoHuiMiMa2;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String String_uname = shijian_zhaoHuiMiMa1.txt_uname.getText().trim();
String String_wenti = shijian_zhaoHuiMiMa1.txt_wenti.getText().trim();
String String_daan = shijian_zhaoHuiMiMa1.txt_daan.getText().trim();
String sql = "select * from yonghu where yh_Uname='" + String_uname
+ "'and yh_Q='" + String_wenti + "'and yh_A='" + String_daan
+ "'";
ResultSet res = DBUtils.Select(sql);
try {
if (res.next()) {
new chongzhimimajiemian();
gongjvClass.uname = String_uname;
} else {
// JOptionPane.showMessageDialog(null,"XXX")是用來提示用戶答案輸入錯誤的格式
JOptionPane.showMessageDialog(null, "答案輸入錯誤");
shijian_zhaoHuiMiMa1.lb_daan1.setText("答案輸入錯誤");
shijian_zhaoHuiMiMa1.lb_daan1.setForeground(Color.red);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void focusGained(FocusEvent arg0) {
// focusGained(FocusEvent arg0)是 失去焦點事件的格式
}
@Override
public void focusLost(FocusEvent arg0) {
String uname = shijian_zhaoHuiMiMa1.txt_uname.getText().trim();
String sql = "select * from yonghu where yh_uname='" + uname + "'";
ResultSet res = DBUtils.Select(sql);
try {
if (res.next()) {
String shiJian_wenti = res.getString("yh_Q");
shijian_zhaoHuiMiMa1.txt_wenti.setText(shiJian_wenti);
shijian_zhaoHuiMiMa1.lb_uname1.setText("");
} else {
shijian_zhaoHuiMiMa1.lb_uname1.setText("用戶名錯誤");
shijian_zhaoHuiMiMa1.lb_uname1.setForeground(Color.red);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class zhaoHuiMiMa extends JFrame {
static JButton btn_queren, btn_quxiao = null;
static JLabel lb_uname, lb_wenti, lb_daan = null;
static JLabel lb_uname1, lb_wenti1, lb_daan1 = null;
/*
* 找回密碼思路:1.寫一個文本框用來填用戶名,用戶名需要添加焦點獲取事件,如果失去焦點后去數(shù)據(jù)庫查詢用戶輸入
* 的用戶名是否存在,如果存在就將其問題查詢出來,添加到第二 個文本框上(第二個文本框是不能手動輸
* 入的),讓用戶輸入答案,輸入完畢后點擊確定按鈕時判斷用戶名,問題和答案是否一致, 如果一致說明 用戶輸入的答案是正確的, 之后跳到重置密碼界面
*/
static JTextField txt_uname, txt_wenti, txt_daan = null;
public zhaoHuiMiMa() {
// 基本必須的內(nèi)容:布局方式、大小居中、釋放資源(找回密碼頁面不需要釋放資源)、顯示
// 布局方式
this.setTitle("找回密碼頁面");
this.setLayout(null);
// 大小居中
this.setSize(500, 500);
this.setLocationRelativeTo(null);
lb_uname = new JLabel("用戶名");
lb_wenti = new JLabel("問題");
lb_daan = new JLabel("答案");
lb_uname1 = new JLabel("請輸入用戶名");
lb_daan1 = new JLabel("請輸入答案");
txt_uname = new JTextField();
txt_wenti = new JTextField();
txt_daan = new JTextField();
txt_uname.addFocusListener(new shijian_zhmm(this));
lb_uname1.setForeground(Color.blue);
lb_daan1.setForeground(Color.blue);
lb_uname.setBounds(100, 100, 100, 30);
lb_wenti.setBounds(100, 140, 100, 30);
lb_daan.setBounds(100, 180, 100, 30);
lb_uname1.setBounds(310, 105, 100, 30);
lb_daan1.setBounds(310, 185, 100, 30);
this.add(lb_uname);
this.add(lb_wenti);
this.add(lb_daan);
this.add(lb_uname1);
this.add(lb_daan1);
txt_uname.setBounds(210, 100, 100, 30);
txt_wenti.setBounds(210, 140, 100, 30);
txt_wenti.setEditable(false);// 隱藏
txt_daan.setBounds(210, 180, 100, 30);
this.add(txt_uname);
this.add(txt_wenti);
this.add(txt_daan);
btn_queren = new JButton("確認且回答正確后跳轉(zhuǎn)重置密碼頁面");
btn_queren.addActionListener(new shijian_zhmm(this));
btn_quxiao = new JButton("取消");
btn_queren.setBounds(10, 230, 290, 30);
btn_quxiao.setBounds(340, 230, 70, 30);
this.add(btn_queren);
this.add(btn_quxiao);
// 顯示
this.setVisible(true);
}
}