欢迎光临散文网 会员登陆 & 注册

Java swing图书管理系统5(个人设计思考得出的全功能简易原版)

2020-04-19 21:42 作者:诗书画唱  | 我要投稿


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;

// 删除成功再查询一遍表格就可以实现表格的刷新,但是我们的查询写在了构造方法里,内容很多,

// 这里想要使用又要在写一遍,很麻烦,那么现在我们写一个方法 将查询内容封装进去,再想查询的

// 时候只要调用查询的方法即可


} else {


JOptionPane.showMessageDialog(null, "出现了未知的错误,请重试");

}

}

}

}


@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

}


// 先布局左侧内容, 数据库查询,提取出-一个查询的方法

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();

// 判断这个内容,给相应的控件设置

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, "出现了未知的错误,请重试!");

}

}

}

}


@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.先做表格的查询内容

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);

// 查询表格的内容的方法

users_Load(null, null);

}


// 构建一个查询的方法

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>>();

// 这个存储的是内容

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.写一个文本框用来填用户名,用户名需要添加焦点获取事件,如果失去焦点后去数据库查询用户输入

* 的用户名是否存在,如果存在就将其问题查询出来,添加到第二 个文本框上(第二个文本框是不能手动输

* 入的),让用户输入答案,输入完毕后点击确定按钮时判断用户名,问题和答案是否一致, 如果一致说明 用户输入的答案是正确的, 之后跳到重置密码界面

*/

static JTextField txt_uname, txt_wenti, txt_daan = null;


public zhaoHuiMiMa() {

// 基本必须的内容:布局方式、大小居中、释放资源(找回密码页面不需要释放资源)、显示

// 布局方式

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("确认且回答正确后跳转重置密码页面");

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);


}

}


Java swing图书管理系统5(个人设计思考得出的全功能简易原版)的评论 (共 条)

分享到微博请遵守国家法律