基于java和mysql的swing+mysql图书管理系统(java+swing+gui+mysql)

私信源码获取及调试交流

私信源码获取及调试交流

运行环境

Java≥8、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于java的Swing+MySQL图书管理系统(java+swing+gui+mysql)

功能介绍:借阅列表、图书类别管理、图书馆里、用户管理、借阅管理、关于我们

		scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setBounds(31, 113, 456, 143);
		getContentPane().add(scrollPane);

		bookTypeTable = new JTable();
		bookTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				int row = bookTypeTable.getSelectedRow();
				idTxt.setText((String) bookTypeTable.getValueAt(row, 0));
				bookTypeNameTxt.setText((String) bookTypeTable.getValueAt(row,
						1));
				bookTypeDescTxt.setText((String) bookTypeTable.getValueAt(row,
						2));
			}
		});
		bookTypeTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u7F16\u53F7",
						"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0",
						"\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0" }) {
			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(107);
		bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(243);
		bookTypeTable.setToolTipText("\u56FE\u4E66\u7C7B\u522B");
		bookTypeTable.setSurrendersFocusOnKeystroke(true);
		scrollPane.setViewportView(bookTypeTable);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setToolTipText("\u8868\u5355\u64CD\u4F5C");
		panel.setBounds(31, 282, 456, 196);
		getContentPane().add(panel);
		panel.setLayout(null);

		JLabel label = new JLabel("\u7F16\u53F7\uFF1A");
		label.setBounds(10, 44, 50, 23);
		panel.add(label);

		idTxt = new JTextField();
		idTxt.setEditable(false);
		idTxt.setBounds(71, 45, 72, 21);
				if (StringUtil.isEmpty(author)) {
					JOptionPane.showMessageDialog(null, "图书作者不能为空!");
					return;
				}
				if (StringUtil.isEmpty(price)) {
					JOptionPane.showMessageDialog(null, "图书价格不能为空!");
					return;
				}

				BookType bookType = (BookType) jcb_bookType.getSelectedItem();
				int bookTypeId = bookType.getId();

				Book book = new Book(Integer.parseInt(id), bookName, author,
						Float.parseFloat(price), bookDesc, bookTypeId);
				Connection con = null;
				try {
					con = dbUtil.getCon();
					int modifyNum = bookDao.bookModify(con, book);
					if (modifyNum == 1) {
						JOptionPane.showMessageDialog(null, "修改成功");
						resetValue();
						fillTable(new Book());
					} else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(null, "修改失败");
				} finally {
					try {
						dbUtil.closeCon(con);
					} catch (Exception ec) {
						ec.printStackTrace();
					}
				}
			}
		});
		jb_modify.setBounds(66, 225, 93, 23);
		panel_1.add(jb_modify);
		// 删除按钮
		JButton jb_delete = new JButton("\u5220\u9664");
		jb_delete.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\delete.png"));
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = idTxt.getText();
				if (StringUtil.isEmpty(id)) {
					JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
					return;
				}
				int n = JOptionPane.showConfirmDialog(null, "确定要删除这条记录吗?");
				if (n == 0) {
					Connection con = null;
					try {
		getContentPane().add(lb_bookType);

		jcb_bookType = new JComboBox();
		jcb_bookType.setBounds(304, 97, 101, 21);
		getContentPane().add(jcb_bookType);

		JLabel lb_bookDesc = new JLabel("\u56FE\u4E66\u7B80\u4ECB\uFF1A");
		lb_bookDesc.setBounds(28, 176, 73, 15);
		getContentPane().add(lb_bookDesc);

		bookDescTxt = new JTextArea();
		bookDescTxt.setBounds(112, 172, 297, 127);
		getContentPane().add(bookDescTxt);
		// 添加图书按钮
		JButton jb_bookAdd = new JButton("\u6DFB\u52A0");
		jb_bookAdd.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\add.png"));
		jb_bookAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookName = bookNameTxt.getText();
				String bookAuthor = bookAuthorTxt.getText();
				String bookPrice = bookPriceTxt.getText();
				String bookDesc = bookDescTxt.getText();
				if (StringUtil.isEmpty(bookName)) {
					JOptionPane.showMessageDialog(null, "图书名称不能为空");
					return;
				}
				if (StringUtil.isEmpty(bookAuthor)) {
					JOptionPane.showMessageDialog(null, "图书作者不能为空");
					return;
				}
				if (StringUtil.isEmpty(bookPrice)) {
					JOptionPane.showMessageDialog(null, "图书价格不能为空");
					return;
				}
				BookType bookType = (BookType) jcb_bookType.getSelectedItem();
				int bookTypeId = bookType.getId();
				Book book = new Book(-1, bookName, bookAuthor, Float
						.parseFloat(bookPrice), bookDesc, bookTypeId);
				Connection con = null;
				try {
					con = dbUtil.getCon();
					int addNum = BookDao.bookAdd(con, book);
					if (1 == addNum) {
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void init() {
		JLabel lb_bookTypeName = new JLabel(
				"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0\uFF1A");
		lb_bookTypeName.setBounds(31, 61, 93, 15);
		getContentPane().add(lb_bookTypeName);

		s_bookTypeNameTxt = new JTextField();
		s_bookTypeNameTxt.setBounds(134, 58, 185, 21);
		getContentPane().add(s_bookTypeNameTxt);
		s_bookTypeNameTxt.setColumns(10);
		// 搜索按钮
		JButton jb_search = new JButton("\u67E5\u8BE2");
		jb_search.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\search.png"));
		jb_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookTypeName = s_bookTypeNameTxt.getText();
				BookType bookType = new BookType();
				bookType.setBookTypeName(bookTypeName);
				fillTable(bookType);
			}
		});
		jb_search.setBounds(379, 57, 108, 23);
		getContentPane().add(jb_search);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {

			}
		});
		scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
		scrollPane.setBounds(31, 113, 456, 143);
		});
		jb_search.setBounds(510, 16, 93, 23);
		panel.add(jb_search);
		panel.setFocusTraversalPolicy(new FocusTraversalOnArray(
				new Component[] { lblid, userIdTxt, lblid_1, bookIdTxt,
						jb_search }));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 92, 655, 174);
		getContentPane().add(scrollPane);

		// 借书记录表格
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				int row = table.getSelectedRow();
				s_idTxt.setText(table.getValueAt(row, 0) + "");

				String userName = table.getValueAt(row, 1) + "";
				s_userNameTxt.setText(userName.replaceAll("\\(\\d*\\)", ""));
				String bookName = table.getValueAt(row, 2) + "";
				s_bookNameTxt.setText(bookName.replaceAll("\\(\\d*\\)", ""));

			}
		});
		table.setModel(new DefaultTableModel(new Object[][] { { null, null,
				null }, }, new String[] { "\u7F16\u53F7", "\u501F\u9605\u4EBA",
				"\u501F\u9605\u56FE\u4E66" }));
		table.getColumnModel().getColumn(0).setPreferredWidth(83);
		table.getColumnModel().getColumn(1).setPreferredWidth(162);
		table.getColumnModel().getColumn(2).setPreferredWidth(187);
		scrollPane.setViewportView(table);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 276, 655, 101);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);

		JLabel label_2 = new JLabel("\u7F16\u53F7\uFF1A");
		label_2.setBounds(10, 33, 60, 15);
		panel_1.add(label_2);

		s_idTxt = new JTextField();
		s_idTxt.setBounds(91, 30, 66, 21);
		panel_1.add(s_idTxt);
		s_idTxt.setColumns(10);
		scrollPane.setBounds(31, 113, 456, 143);
		getContentPane().add(scrollPane);

		bookTypeTable = new JTable();
		bookTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				int row = bookTypeTable.getSelectedRow();
				idTxt.setText((String) bookTypeTable.getValueAt(row, 0));
				bookTypeNameTxt.setText((String) bookTypeTable.getValueAt(row,
						1));
				bookTypeDescTxt.setText((String) bookTypeTable.getValueAt(row,
						2));
			}
		});
		bookTypeTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u7F16\u53F7",
						"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0",
						"\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0" }) {
			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(107);
		bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(243);
		bookTypeTable.setToolTipText("\u56FE\u4E66\u7C7B\u522B");
		bookTypeTable.setSurrendersFocusOnKeystroke(true);
		scrollPane.setViewportView(bookTypeTable);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setToolTipText("\u8868\u5355\u64CD\u4F5C");
		panel.setBounds(31, 282, 456, 196);
		getContentPane().add(panel);
		panel.setLayout(null);

		JLabel label = new JLabel("\u7F16\u53F7\uFF1A");
		label.setBounds(10, 44, 50, 23);
		panel.add(label);


public class LendBookManageInterFrm extends JInternalFrame {
	DbUtil dbUtil = new DbUtil();
	UserDao userDao = new UserDao();
	BookDao bookDao = new BookDao();

	private JTextField userIdTxt;
	private JTextField bookIdTxt;
	private JTable table;
	private JTextField s_idTxt;
	private JTextField s_userNameTxt;
	private JTextField s_bookNameTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LendBookManageInterFrm frame = new LendBookManageInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LendBookManageInterFrm() {
		this.idTxt.setText("");
		this.bookNameTxt.setText("");
		this.bookAuthorTxt.setText("");
		this.bookPriceTxt.setText("");
		this.bookDescTxt.setText("");
		if (jcb_bookType.getItemCount() > 0) {
			jcb_bookType.setSelectedIndex(0);
		}
	}
}

public class UserMainFrm extends JFrame {
		jmi_bookManage.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\bookManager.png"));
		jmi_bookManage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookManageInterFrm bookManageInterFrm = new BookManageInterFrm();
				bookManageInterFrm.setVisible(true);
				table.add(bookManageInterFrm);
			}
		});
		menu_1.add(jmi_bookManage);

		JMenuItem menu_2 = new JMenuItem("\u9000\u51FA");
		menu_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		mn_datamanager.add(menu_2);

		JMenu mn_lendreturn = new JMenu("\u501F\u9605\u7BA1\u7406");
		menuBar.add(mn_lendreturn);

		JMenuItem menuItem = new JMenuItem(
				"\u56FE\u4E66\u501F\u9605\u7BA1\u7406");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LendBookManageInterFrm lendBookManageInterFrm = new LendBookManageInterFrm();
				lendBookManageInterFrm.setVisible(true);
				table.add(lendBookManageInterFrm);
			}
		});
		mn_lendreturn.add(menuItem);

		JMenu mnNewMenu_2 = new JMenu("\u5173\u4E8E\u6211\u4EEC");
		menuBar.add(mnNewMenu_2);
		// 关于我们
		JMenuItem menuItem_1 = new JMenuItem("\u5173\u4E8E\u8F6F\u4EF6");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane
						.showMessageDialog(
								null,
								"**********************************************\n"
										+ " 编写者:围观客 "
										+ " 网站地址:http://www.weiguanke.cn"
										+ " 谢谢大家使用我门的软件");
			}
		});
		mnNewMenu_2.add(menuItem_1);
	}
				} finally {
					try {
						dbUtil.closeCon(con);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		jb_modify.setBounds(71, 156, 90, 23);
		panel.add(jb_modify);
		// 删除按钮
		JButton jb_delete = new JButton("\u5220\u9664");
		jb_delete.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\delete.png"));
		jb_delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = idTxt.getText();
				Connection con = null;
				try {
					con = dbUtil.getCon();
					int re = bookTypeDao.bookTypeDelete(con, id);
					if (1 == re) {
						JOptionPane.showMessageDialog(null, "删除成功");
						fillTable(new BookType("", ""));
						reset();
					} else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "删除失败");
					e1.printStackTrace();
				} finally {
					try {
						dbUtil.closeCon(con);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		jb_delete.setBounds(215, 156, 90, 23);
		panel.add(jb_delete);
		getContentPane().setFocusTraversalPolicy(
				new FocusTraversalOnArray(new Component[] { lb_bookTypeName,
						s_bookTypeNameTxt, jb_search, scrollPane, panel, label,
						idTxt, label_1, bookTypeNameTxt, label_2,
						bookTypeDescTxt, jb_modify, jb_delete }));
	}

	private void reset() {
		idTxt.setText("");
		bookTypeNameTxt.setText("");
		bookTypeDescTxt.setText("");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 填充表格
	private void fillTable(Book book) {
		DefaultTableModel dtm = (DefaultTableModel) bookTable.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = bookDao.bookList(con, book);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getInt("id"));
				v.add(rs.getString("bookName"));
				v.add(rs.getString("author"));
				v.add(rs.getFloat("price"));
				v.add(rs.getString("bookDesc"));
				v.add(rs.getString("bookTypeName"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void init() {
		scrollPane = new JScrollPane();
		scrollPane.setToolTipText("");
		scrollPane.setBounds(30, 91, 732, 178);
		getContentPane().add(scrollPane);

				String userName = userNameTxt.getText();
				String userPassword = new String(userPasswordTxt.getPassword());
				if (StringUtil.isEmpty(userName)) {
					JOptionPane.showMessageDialog(null, "用户名不能为空");
					return;
				}
				if (StringUtil.isEmpty(userPassword)) {
					JOptionPane.showMessageDialog(null, "密码不能为空");
					return;
				}
				User user = new User(userName, userPassword);
				Connection con = null;

				try {
					con = dbUtil.getCon();
					User reUser = userDao.login(con, user);
					if (reUser != null) {
						dispose();// 销毁登录框
						if (1 == reUser.getRole()) {
							new MainFrm().setVisible(true);// 创建管理员主界面Firme
						} else if (0 == reUser.getRole()) {
							new UserMainFrm(reUser).setVisible(true); // 创建读者界面
						}
					} else {
						JOptionPane.showMessageDialog(null, "用户名或密码不正确");
					}
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "登录失败");
					e1.printStackTrace();
				} finally {
					try {
						con.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}

			}
		});
		jb_login.setBounds(102, 191, 93, 23);
		contentPane.add(jb_login);
		// 重置按钮
		JButton jb_reset = new JButton("\u91CD\u7F6E");
		jb_reset.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\reset.png"));
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

		bookAuthorTxt = new JTextField();
		bookAuthorTxt.setColumns(10);
		bookAuthorTxt.setBounds(299, 78, 118, 21);
		panel_1.add(bookAuthorTxt);

		JLabel label_4 = new JLabel("\u56FE\u4E66\u4EF7\u683C\uFF1A");
		label_4.setBounds(10, 160, 76, 15);
		panel_1.add(label_4);

		bookPriceTxt = new JTextField();
		bookPriceTxt.setBounds(96, 157, 117, 21);
		panel_1.add(bookPriceTxt);
		bookPriceTxt.setColumns(10);

		JLabel label_5 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
		label_5.setBounds(224, 160, 65, 15);
		panel_1.add(label_5);

		jcb_bookType = new JComboBox<BookType>();
		jcb_bookType.setBounds(299, 157, 118, 21);
		panel_1.add(jcb_bookType);

		JLabel label_6 = new JLabel("\u56FE\u4E66\u63CF\u8FF0\uFF1A");
		label_6.setBounds(439, 81, 65, 15);
		panel_1.add(label_6);

		bookDescTxt = new JTextArea();
		bookDescTxt.setBounds(514, 77, 208, 98);
		panel_1.add(bookDescTxt);
		// 修改按钮
		JButton jb_modify = new JButton("\u4FEE\u6539");
		jb_modify.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\modify.png"));
		jb_modify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String id = idTxt.getText();
				if (StringUtil.isEmpty(id)) {
					JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
					return;
				}
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane
						.showMessageDialog(
								null,
								"**********************************************\n"
										+ " 编写者:围观客 "
										+ " 网站地址:http://www.weiguanke.cn"
										+ " 谢谢大家使用我门的软件");
			}
		});
		mnNewMenu_2.add(menuItem_1);
	}
}

public class LogonFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTxt;
	private JPasswordField userPasswordTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogonFrm frame = new LogonFrm();
					frame.setVisible(true);
				} catch (Exception e) {
						JOptionPane.showMessageDialog(null, "删除失败");
						e1.printStackTrace();
					}
				}
			}
		});
		jb_delete.setBounds(257, 225, 93, 23);
		panel_1.add(jb_delete);
	}

	private void resetValue() {
		this.idTxt.setText("");
		this.bookNameTxt.setText("");
		this.bookAuthorTxt.setText("");
		this.bookPriceTxt.setText("");
		this.bookDescTxt.setText("");
		if (jcb_bookType.getItemCount() > 0) {
			jcb_bookType.setSelectedIndex(0);
		}
	}
}

						s_bookTypeNameTxt, jb_search, scrollPane, panel, label,
						idTxt, label_1, bookTypeNameTxt, label_2,
						bookTypeDescTxt, jb_modify, jb_delete }));
	}

	private void reset() {
		idTxt.setText("");
		bookTypeNameTxt.setText("");
		bookTypeDescTxt.setText("");
	}
}

public class LendBookManageInterFrm extends JInternalFrame {
	DbUtil dbUtil = new DbUtil();
	UserDao userDao = new UserDao();
	BookDao bookDao = new BookDao();

	private JTextField userIdTxt;
		fillTable(new BookType("", ""));
	}

	// 填充表格
	private void fillTable(BookType bookType) {
		DefaultTableModel dtm = (DefaultTableModel) bookTypeTable.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet res = bookTypeDao.bookTypeList(con, bookType);
			while (res.next()) {
				Vector v = new Vector();
				v.add(res.getString("id"));
				v.add(res.getString("bookTypeName"));
				v.add(res.getString("bookTypeDesc"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void init() {
		JLabel lb_bookTypeName = new JLabel(
				"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0\uFF1A");
		lb_bookTypeName.setBounds(31, 61, 93, 15);
		getContentPane().add(lb_bookTypeName);

		s_bookTypeNameTxt = new JTextField();
		s_bookTypeNameTxt.setBounds(134, 58, 185, 21);
		getContentPane().add(s_bookTypeNameTxt);
		s_bookTypeNameTxt.setColumns(10);
		// 搜索按钮
		JButton jb_search = new JButton("\u67E5\u8BE2");
		jb_search.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\search.png"));

	private void init() {
		DbUtil dbUtil = new DbUtil();
		UserDao userDao = new UserDao();
		JLabel lb_userName = new JLabel("\u7528\u6237\u540D\uFF1A");
		lb_userName.setBounds(102, 75, 54, 15);
		contentPane.add(lb_userName);

		userNameTxt = new JTextField();
		userNameTxt.setBounds(166, 72, 152, 21);
		contentPane.add(userNameTxt);
		userNameTxt.setColumns(10);

		JLabel lb_userPassword = new JLabel("\u5BC6  \u7801\uFF1A");
		lb_userPassword.setBounds(102, 129, 54, 15);
		contentPane.add(lb_userPassword);

		// 登录按钮
		JButton jb_login = new JButton("\u767B\u5F55");
		jb_login.setIcon(new ImageIcon(
				"D:\\java\\MyBookManager\\image\\login.png"));
		jb_login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName = userNameTxt.getText();
				String userPassword = new String(userPasswordTxt.getPassword());
				if (StringUtil.isEmpty(userName)) {
					JOptionPane.showMessageDialog(null, "用户名不能为空");
					return;
				}
				if (StringUtil.isEmpty(userPassword)) {
					JOptionPane.showMessageDialog(null, "密码不能为空");
					return;
				}
				User user = new User(userName, userPassword);
				Connection con = null;

				try {
					con = dbUtil.getCon();
					User reUser = userDao.login(con, user);
					if (reUser != null) {
						dispose();// 销毁登录框

请添加图片描述
请添加图片描述
请添加图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐