基于java设计的图书管理系统

小编在学java的期间,经过查阅资料经修改,实现的一些简单功能:

1、登录界面:

用户可以选择登录或注册账号,如果初始没有账号,可以通过注册按钮进行注册,然后在进行登录:

代码:

public class SignMenu {
	private String username;
	private String password1;
	private String text;
	private int flag = 0;
	public JTextField id;
	private String name;
	private String select;

	public void init() {
		JFrame f = new JFrame("欢迎使用图书管理系统");
		// 改变窗口图标
		Toolkit t = Toolkit.getDefaultToolkit();
		Image img = t.getImage("src\\menu\\sign.jpg");
		f.setIconImage(img);

		// 设置窗口大小
		f.setSize(600, 600);

		// 使窗口居中
		f.setLocationRelativeTo(null);

		// 设置布局为空
		f.setLayout(null);

		// 设置窗口背景图案
		Icon i = new ImageIcon("src\\menu\\background4.jpg");
		JLabel jLable = new JLabel(i);
		jLable.setBounds(0, 0, 600, 600);

		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		JPanel panel6 = new JPanel();

		/* 添加大标题:图书管理系统 */
		JLabel lable = new JLabel("图书管理系统");
		Font font = new Font("宋体", Font.BOLD, 40);// 设置字体
		lable.setFont(font);
		panel1.add(lable);
		panel1.setBounds(140, 60, 300, 300);// 设置面板大小
		f.add(panel1);

		/* 用户类型标签 */
		JLabel user = new JLabel("用户类型  ");
		Font font1 = new Font("宋体", Font.BOLD, 25);
		user.setFont(font1);
		panel2.add(user);

		/* 用户类型的下拉框 */
		JComboBox<String> j = new JComboBox<String>();
		Dimension dimension = new Dimension(200, 30);// 框框的大小
		j.setPreferredSize(dimension);
		j.addItem("普通用户");
		j.addItem("管理员用户");
		Font font2 = new Font("宋体", Font.BOLD, 15);
		j.setFont(font2);
		j.setBackground(Color.PINK);
		panel2.setBounds(90, 160, 400, 300);
		panel2.add(j);
		f.add(panel2);

		/* 账号标签 */
		JLabel labid = new JLabel("   账号: ");
		Font font3 = new Font("宋体", Font.BOLD, 25);
		labid.setFont(font3);
		panel3.add(labid);

		/* 输入账户的文本框 */
		id = new JTextField();
		id.setPreferredSize(dimension);
		// id.setBackground(Color.PINK);
		panel3.add(id);
		panel3.setBounds(90, 230, 400, 300);
		f.add(panel3);

		/* 密码标签 */
		JLabel labpassword = new JLabel("   密码: ");
		Font font4 = new Font("宋体", Font.BOLD, 25);
		labpassword.setFont(font4);
		panel4.add(labpassword);

		/* 输入密码文本框 */
		JPasswordField password = new JPasswordField();
		password.setPreferredSize(dimension);
		Font font5 = new Font("宋体", Font.BOLD, 25);
		password.setFont(font5);
		password.setBackground(Color.pink);
		panel4.add(password);
		panel4.setBounds(90, 290, 400, 300);
		f.add(panel4);

		/* 注册登陆按钮 */
		JButton button1 = new JButton("注册");
		JButton button2 = new JButton("登陆");
		button1.setFont(font4);
		button2.setFont(font4);
		Dimension dimension2 = new Dimension(100, 50);
		button1.setPreferredSize(dimension2);
		button2.setPreferredSize(dimension2);
		button1.setBackground(Color.PINK);
		button2.setBackground(Color.PINK);
		panel5.add(button1);
		panel6.add(button2);
		panel5.setBounds(150, 370, 150, 400);
		panel6.setBounds(330, 370, 150, 400);
		f.add(panel5);
		f.add(panel6);

		id.setOpaque(false);
		password.setOpaque(false);
		panel1.setOpaque(false);
		panel2.setOpaque(false);
		panel3.setOpaque(false);
		panel4.setOpaque(false);
		panel5.setOpaque(false);
		panel6.setOpaque(false);

		f.add(jLable);

		/* 用户类型 */
		select = "普通用户";
		j.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					select = (String) e.getItem();
				}
			}
		});

		/* 点击注册跳转到注册界面 */
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				f.dispose();
				Regest r = new Regest();
				try {
					r.show();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		/* 登陆信息比对 */
		button2.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				setText(j.getToolTipText());
				username = id.getText().trim();
				password1 = password.getText().trim();

				try {
					flag = select(username, password1);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (flag == 1) {// 密码输入正确
					name = id.getText().trim();

					if (select == "普通用户") {
						f.dispose();
						new User(name);
					} else if (select == "管理员用户") {
						flag = 0;
						try {
							flag = ensure(name);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						if (flag == 1) {
							new Admin(name);
							f.dispose();
						} else {
							JOptionPane.showMessageDialog(null, "对不起,您还不是管理员", "警告", JOptionPane.WARNING_MESSAGE);
						}
					}
				} else if (flag == 0) {// 密码输入错误
					JOptionPane.showMessageDialog(null, "密码输入有误", "抱歉", JOptionPane.PLAIN_MESSAGE);
				} else if (flag == 2) {
					JOptionPane.showMessageDialog(null, "用户名不存在", "注意", JOptionPane.PLAIN_MESSAGE);
				}
			}

			private void setText(String toolTipText) {
				// TODO Auto-generated method stub
				
			}
		});

		// 设置不可改变窗口大小
		f.setResizable(false);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

2、注册: 

注册要求对密码进行限制,只能是6~12位的数字和字符组成,用正则表达式进行判断,增强了密码的安全性,如果哪一项为空,就会弹出相应的提示框:

代码:

public class Regest {
	private String username;
	private String password;
	private String repassword;
	private String name;
	
	public void show() throws Exception {
		JFrame f = new JFrame("注册"); 
		//改变窗口图标
		Toolkit tool = Toolkit.getDefaultToolkit();
		Image img = tool.getImage("src\\menu\\sign.jpg");
		f.setIconImage(img);
			
		f.setSize(400, 500);
		f.setLocationRelativeTo(null);
		f.setLayout(null);
		
		/*设置窗口背景图案*/
		Icon i = new ImageIcon("src\\menu\\regist.jpg");
		JLabel label = new JLabel(i);
		label.setBounds(0, 0, 400, 500);

		JPanel panel1 = new JPanel();
		JPanel panel2 = new JPanel();
		JPanel panel3 = new JPanel();
		JPanel panel4 = new JPanel();
		JPanel panel5 = new JPanel();
		JPanel panel6 = new JPanel();
		@SuppressWarnings("unused")
		JPanel panel7 = new JPanel();
		
		//界面文字的字体,字形,字号
		Font font = new Font("宋体",Font.BOLD,15);
		//界面框框的字体,字形,字号
		Font font2 = new Font("宋体",Font.BOLD,25);
		Dimension dimension = new Dimension(200,30);
		
		panel1.setLayout(null);
		/*返回按钮*/
		JButton rebutton = new JButton("返回");
		Font font4 = new Font("宋体",Font.BOLD,12);
		rebutton.setFont(font4);
		Dimension dimension1 = new Dimension(60,40);
		rebutton.setPreferredSize(dimension1);
		rebutton.setBackground(Color.white);
		rebutton.setBounds(0, 0, 60, 40);
		panel1.add(rebutton);
		f.add(panel1);
			
		//添加注册大标题
		JLabel regist = new JLabel("注册");
		Font font1 = new Font("宋体",Font.BOLD,36);
		regist.setFont(font1);
		regist.setBounds(150, 10, 400, 70);
		panel1.add(regist);
		panel1.setBounds(0, 0, 400, 70);		
		
		//添加用户名标签
		JLabel labname = new JLabel(" 用户名:");
		labname.setFont(font);
		panel2.add(labname);
		
		//添加用户名文本框
		JTextField textname = new JTextField();
		textname.setPreferredSize(dimension);
		panel2.add(textname);
		panel2.setBounds(10, 90, 300, 70);
		f.add(panel2);
		
		/*添加设置密码标签*/
		JLabel labpassword = new JLabel("设置密码:");
		labpassword.setFont(font);
		panel3.add(labpassword);
		
		/*添加设置密码的文本框*/
		JPasswordField textpassword = new JPasswordField();
		textpassword.setPreferredSize(dimension);
		textpassword.setFont(font2);
		panel3.add(textpassword);
		JLabel labrequest = new JLabel("         *密码由6-12位数字、字符组成*");
		Font font3 = new Font("宋体",Font.BOLD,12);
		labrequest.setFont(font3);
		labrequest.setForeground(Color.red);
		panel3.add(labrequest);
		panel3.setBounds(10, 150, 300, 70);
		f.add(panel3);
		
		/*重新输入密码标签*/
		JLabel labpassword1 = new JLabel("确认密码:");
		labpassword1.setFont(font);
		panel4.add(labpassword1);
		
		/*重新输入密码文本框*/
		JPasswordField textpassword1 = new JPasswordField();
		textpassword1.setPreferredSize(dimension);
		textpassword1.setFont(font2);
		panel4.add(textpassword1);
		panel4.setBounds(10, 210, 300, 70);
		f.add(panel4);
		
		/*设置姓名标签*/
		JLabel labelname = new JLabel("    姓名:");
		labelname.setFont(font);
		panel5.add(labelname);
		
		/*输入姓名文本框*/
		JTextField textname1 = new JTextField();
		textname1.setPreferredSize(dimension);
		panel5.add(textname1);
		panel5.setBounds(10, 270, 300, 70);
		f.add(panel5);
		
		/*添加注册按钮*/
		JButton button = new JButton("注册");
		Font font5 = new Font("宋体",Font.BOLD,25);
		button.setFont(font5);
		Dimension dimension2 = new Dimension(200,40);
		button.setPreferredSize(dimension2);
		button.setBackground(Color.pink);
		panel6.add(button);
		panel6.setBounds(100, 340, 200, 50);
		f.add(panel6);

3、普通用户主页面:

 普通用户可以进行借阅图书,归还图书以及查看自己的借阅图书历史记录,同时还可以修改用户信息:

4、管理员界面:

同普通用户差不多,管理员用户可以查看所有的借阅图书历史记录,对图书以及用户信息的增、删、查、改等操作:

5、用户管理

6、增加用户

有不足之处,欢迎交流,同时附上源码,回复 “CSDN” ,有问题的私聊小编…… 

发布了145 篇原创文章 · 获赞 203 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/qq_42680327/article/details/104145017