Java实战项目(2):swing图书管理系统的登录,注册,找回密码,增删查,效果,格式等
结构:

(5到8)
5

package denglu;
public class gongjvClass {
public static String uname = "";
}

6

package denglu;
public class main {
public static void main(String[] args) {
new denglu();
// new zhuce();
// new zhmm();
// new zhaohuimima();
}
}

7

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

8

package denglu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class shijian_zhuce implements ActionListener {
public static zhuCe chuangkouneirong2 = null;
public shijian_zhuce(zhuCe shangPinLeiXingXiaLaKuang1) {
this.chuangkouneirong2 = shangPinLeiXingXiaLaKuang1;
// chuangkouneirong窗口内容
}
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "点击了注册按钮");
String String_uname = chuangkouneirong2.JTextField_uname.getText();// 得到用户名
String String_pwd = chuangkouneirong2.JPasswordField_pwd1.getText();// 得到密码
String String_pwd1 = chuangkouneirong2.JPasswordField_pwd2.getText();// 得到确认密码
String String_TureName = chuangkouneirong2.JTextField_name.getText();// 得到用户名
String String_age = chuangkouneirong2.JComboBox_age.getSelectedItem().toString();
// String String_age = zc.JTextField_age.getText();// 得到年龄
String String_sex = "男";
// 判断用户是否选择是女:
if (chuangkouneirong2.rb2.isSelected()) {
String_sex = "女";
}
String String_phone = chuangkouneirong2.JTextField_phone.getText();// 得到电话号码
// String String_address = zc.JTextField_address.getText();// 得到地址
ArrayList<String> jiHe = new ArrayList<String>();
// jiHe集合
if (chuangkouneirong2.address1.isSelected()) {
jiHe.add(chuangkouneirong2.address1.getText());
}
if (chuangkouneirong2.address2.isSelected()) {
jiHe.add(chuangkouneirong2.address2.getText());
}
if (chuangkouneirong2.address3.isSelected()) {
jiHe.add(chuangkouneirong2.address3.getText());
}
String String_address = null;
for (int i = 0; i < jiHe.size(); i++) {
String_address += jiHe.get(i) + ":";// 将集合中的内容转换为字符串
}
String String_jieshao = chuangkouneirong2.JTextArea_jieshao.getText();// 得到介绍
String String_Q = chuangkouneirong2.JTextField_Q.getText();// 得到问题
String String_A = chuangkouneirong2.JTextField_A.getText();// 得到答案
// 判断两次输入的秘密是否一致:
if (!String_pwd.equals(String_pwd1)) {
JOptionPane.showMessageDialog(null, "两次密码输入不一致, 请重新输入");
return;
}
String sql_panduanuname = "select * from yonghu where yh_uname='"
+ String_uname + "'";
ResultSet res_select_uname = DBUtils.Select(sql_panduanuname);
try {
if (res_select_uname.next()) {
JOptionPane.showMessageDialog(null, "用户名已经存在了,请重新输入");
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
String sql = "insert into yonghu values('" + String_uname + "','"
+ String_pwd + "','"
+ String_TureName + "'," + String_age + ",'" + String_sex
+ "','" + String_phone + "','" + String_address + "','"
+ String_jieshao + "','" + String_Q + "','" + String_A + "'"
+ ")";
if (DBUtils.ZSG(sql)) {
JOptionPane.showMessageDialog(null, "注册成功");
} else {
JOptionPane.showMessageDialog(null, "出现了未知的错误,请重试");
}
}
}
public class zhuCe extends JFrame {
// 爱好使用复选框来做
static JCheckBox address1, address2, address3 = null;
public static JButton JButton_zhuce = null;
// 使用下拉框来当年龄
static JComboBox JComboBox_age = null;
public static JPasswordField JPasswordField_pwd1,
JPasswordField_pwd2 = null;
// public static JTextArea jta_adress, jta_jieshao = null;
/* JTextArea类是一 个显示纯文本的多行(DuoHang)区域。 */
static JTextArea JTextArea_jieshao = null;
public static JTextField JTextField_uname, JTextField_name,
JTextField_phone, JTextField_jieshao, JTextField_Q,
JTextField_A = null;
public static JLabel lb_uname, lb_pwd, lb_qrpwd, lb_name, lb_age, lb_sex,
lb_phone, lb_address, lb_jieshao, lb_Q, lb_A = null;
// 性别使用JRadioButton来做
static JRadioButton rb1, rb2 = null;
public zhuCe() {
this.setLayout(null);
this.setSize(600, 700);
this.setTitle("注册界面");
this.setLocationRelativeTo(null);
lb_uname = new JLabel("用户名");
lb_pwd = new JLabel("用户密码");
lb_qrpwd = new JLabel("确认密码");
lb_name = new JLabel("真实姓名");
lb_age = new JLabel("年龄");
lb_sex = new JLabel("性别");
lb_phone = new JLabel("电话");
lb_address = new JLabel("地址");
lb_jieshao = new JLabel("介绍");
lb_Q = new JLabel("问题");
lb_A = new JLabel("答案");
lb_uname.setBounds(80, 100, 70, 30);
lb_pwd.setBounds(80, 140, 70, 30);
lb_qrpwd.setBounds(80, 180, 70, 30);
lb_name.setBounds(80, 220, 70, 30);
lb_age.setBounds(80, 260, 70, 30);
lb_sex.setBounds(80, 300, 70, 30);
lb_phone.setBounds(80, 340, 70, 30);
lb_address.setBounds(80, 380, 70, 30);
lb_jieshao.setBounds(80, 420, 70, 30);
lb_Q.setBounds(80, 460, 70, 30);
lb_A.setBounds(80, 500, 70, 30);
this.add(lb_uname);
this.add(lb_pwd);
this.add(lb_qrpwd);
this.add(lb_name);
this.add(lb_age);
this.add(lb_sex);
this.add(lb_phone);
this.add(lb_address);
this.add(lb_jieshao);
this.add(lb_Q);
this.add(lb_A);
JTextField_uname = new JTextField();
JPasswordField_pwd1 = new JPasswordField();
JPasswordField_pwd2 = new JPasswordField();
JTextField_name = new JTextField();
rb1 = new JRadioButton("男", true);
rb2 = new JRadioButton("女");
this.add(rb1);
this.add(rb2);
JComboBox_age = new JComboBox();
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
for (int i = 18; i < 60; i++) {
JComboBox_age.addItem(i);
}
this.add(JComboBox_age);
// JTextField_age = new JTextField();
// JTextField_sex = new JTextField();
JTextField_phone = new JTextField();
// JTextField_address = new JTextField();
address1 = new JCheckBox("中国", true);
address2 = new JCheckBox("日本");
address3 = new JCheckBox("美国");
JTextField_Q = new JTextField();
JTextField_A = new JTextField();
JTextArea_jieshao = new JTextArea();
JTextArea_jieshao.setColumns(8);
JTextArea_jieshao.setRows(4);
JTextArea_jieshao.setLineWrap(true);
this.add(JTextArea_jieshao);
JTextField_uname.setBounds(160, 100, 140, 30);
JPasswordField_pwd1.setBounds(160, 140, 140, 30);
JPasswordField_pwd2.setBounds(160, 180, 140, 30);
JTextField_name.setBounds(160, 220, 140, 30);
// JTextField_age.setBounds(160, 260, 140, 30);
JComboBox_age.setBounds(160, 260, 140, 30);
// JTextField_sex.setBounds(160, 300, 140, 30);
rb1.setBounds(160, 300, 40, 30);
rb2.setBounds(200, 300, 40, 30);
JTextField_phone.setBounds(160, 340, 140, 30);
// JTextField_address.setBounds(160, 380, 140, 30);
address1.setBounds(160, 380, 80, 30);
address2.setBounds(240, 380, 70, 30);
address3.setBounds(320, 380, 70, 30);
JTextArea_jieshao.setBounds(160, 420, 140, 40);
JTextField_Q.setBounds(160, 460, 140, 30);
JTextField_A.setBounds(160, 500, 140, 30);
this.setLocationRelativeTo(null);
this.add(JTextField_uname);
this.add(JPasswordField_pwd1);
this.add(JPasswordField_pwd2);
this.add(JTextField_name);
// this.add(JTextField_age);
// this.add(JTextField_sex);
this.add(JTextField_phone);
// this.add(JTextField_address);
this.add(JTextField_Q);
this.add(JTextField_A);
this.add(address1);
this.add(address2);
this.add(address3);
JButton_zhuce = new JButton("确认注册");
JButton_zhuce.addActionListener(new shijian_zhuce(this));
JButton_zhuce.setBounds(100, 580, 120, 30);
this.add(JButton_zhuce);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

效果:













