小編在學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” ,有問題的私聊小編……