国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - JAVA實現簡單系統登陸注冊模塊

JAVA實現簡單系統登陸注冊模塊

2019-12-28 14:37lixiyu JAVA教程

這篇文章主要介紹了一個簡單完整的登陸注冊模塊的實現過程,文章條理清晰,在實現過程中加深了對相關概念的理解,需要的朋友可以參考下

前期準備
首先要先明確有個大體的思路,要實現什么樣的功能,了解完成整個模塊要運用到哪些方面的知識,以及從做的過程中去發現自己的不足。技術方面的進步大都都需要從實踐中出來的。
功能:用戶注冊功能+系統登錄功能+生成驗證碼
知識:窗體設計、數據庫設計、JavaBean封裝屬性、JDBC實現對數據庫的連接、驗證碼(包括彩色驗證碼)生成技術,還有就些比如像使用正則表達式校驗用戶注冊信息、隨機獲得字符串、對文本可用字符數的控制等
設計的模塊預覽圖:

JAVA實現簡單系統登陸注冊模塊

JAVA實現簡單系統登陸注冊模塊

JAVA實現簡單系統登陸注冊模塊

彩色驗證碼預覽圖:

JAVA實現簡單系統登陸注冊模塊

所用數據庫:MySQL

 

數據庫設計

創建一個數據庫db_database01,其中包含一個表格tb_user,用來保存用戶的注冊的數據。
其中包含4個字段
id int(11)
username varchar(15)
password varchar(20)
email varchar(45)

MySQL語句可以這樣設計:

?
1
2
3
4
5
6
7
8
9
create schema db_database01;
use db_database01;
create table tb_user(
id int(11) not null auto_increment primary key,
username varchar(15) not null,
password varchar(20) not null,
email varchar(45) not null
);
insert into tb_user values(1,"lixiyu","lixiyu",<a href="mailto:lixiyu419@gmail.com">lixiyu419@gmail.com</a>);

這樣把lixiyu作為用戶名。
select語句檢查一下所建立的表格:

JAVA實現簡單系統登陸注冊模塊

編寫JavaBean封裝用戶屬性

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.lixiyu.model;
public class User {
private int id;// 編號
private String username;// 用戶名
private String password;// 密碼
private String email;// 電子郵箱
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

編寫JDBC工具類

將與數據庫操作相關的代碼放置在DBConfig接口和DBHelper類中
DBConfig接口用于保存數據庫、用戶名和密碼信息
代碼:

?
1
2
3
4
5
6
package com.lixiyu.util;
public interface DBConfig {
String databaseName = "db_database01";// 數據庫名稱
String username = "root";// 數據庫用戶名
String password = "lixiyu";// 數據庫密碼
}

 

為簡化JDBC開發,DBHelper使用了了Commons DbUtil組合。
DBHelper類繼承了DBConfig接口,該類中包含4種方法:
(1)getConnection()方法:獲得數據庫連接,使用MySQL數據源來簡化編程,避免因加載數據庫驅動而發生異常。
(2)exists()方法:判斷輸入的用戶名是否存在。
(3)check()方法:當用戶輸入用戶名和密碼,查詢使用check()方法是否正確。
(4)save()方法:用戶輸入合法注冊信息后,,將信息進行保存。

詳細代碼:

?
1
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.lixiyu.util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang.StringEscapeUtils;
import com.lixiyu.model.User;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class DBHelper implements DBConfig {
 /*
 * 使用MySQL數據源獲得數據庫連接對象
 *
 * @return:MySQL連接對象,如果獲得失敗返回null
 */
 public static Connection getConnection() {
 MysqlDataSource mds = new MysqlDataSource();// 創建MySQL數據源
 mds.setDatabaseName(databaseName);// 設置數據庫名稱
 mds.setUser(username);// 設置數據庫用戶名
 mds.setPassword(password);// 設置數據庫密碼
 try {
 return mds.getConnection();// 獲得連接
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return null;// 如果獲取失敗就返回null
 }
 /*
 * 判斷指定用戶名的用戶是否存在
 *
 * @return:如果存在返回true,不存在或者查詢失敗返回false
 */
 public static boolean exists(String username) {
 QueryRunner runner = new QueryRunner();// 創建QueryRunner對象
 String sql = "select id from tb_user where username = '" + username + "';";// 定義查詢語句
 Connection conn = getConnection();// 獲得連接
 ResultSetHandler<List<Object>> rsh = new ColumnListHandler();// 創建結果集處理類
 try {
 List<Object> result = runner.query(conn, sql, rsh);// 獲得查詢結果
 if (result.size() > 0) {// 如果列表中存在數據
 return true;// 返回true
 } else {// 如果列表中沒有數據
 return false;// 返回false
 }
 } catch (SQLException e) {
 e.printStackTrace();
 } finally {
 DbUtils.closeQuietly(conn);// 關閉連接
 }
 return false;// 如果發生異常返回false
 }
 /*
 * 驗證用戶名和密碼是否正確 使用Commons Lang組件轉義字符串避免SQL注入
 *
 * @return:如果正確返回true,錯誤返回false
 */
 public static boolean check(String username, char[] password) {
 username = StringEscapeUtils.escapeSql(username);// 將用戶輸入的用戶名轉義
 QueryRunner runner = new QueryRunner();// 創建QueryRunner對象
 String sql = "select password from tb_user where username = '" + username + "';";// 定義查詢語句
 Connection conn = getConnection();// 獲得連接
 ResultSetHandler<Object> rsh = new ScalarHandler();// 創建結果集處理類
 try {
 String result = (String) runner.query(conn, sql, rsh);// 獲得查詢結果
 char[] queryPassword = result.toCharArray();// 將查詢到得密碼轉換成字符數組
 if (Arrays.equals(password, queryPassword)) {// 如果密碼相同則返回true
 Arrays.fill(password, '0');// 清空傳入的密碼
 Arrays.fill(queryPassword, '0');// 清空查詢的密碼
 return true;
 } else {// 如果密碼不同則返回false
 Arrays.fill(password, '0');// 清空傳入的密碼
 Arrays.fill(queryPassword, '0');// 清空查詢的密碼
 return false;
 }
 } catch (SQLException e) {
 e.printStackTrace();
 } finally {
 DbUtils.closeQuietly(conn);// 關閉連接
 }
 return false;// 如果發生異常返回false
 }
 /*
 * 保存用戶輸入的注冊信息
 *
 * @return:如果保存成功返回true,保存失敗返回false
 */
 public static boolean save(User user) {
 QueryRunner runner = new QueryRunner();// 創建QueryRunner對象
 String sql = "insert into tb_user (username, password, email) values (?, ?, ?);";// 定義查詢語句
 Connection conn = getConnection();// 獲得連接
 Object[] params = { user.getUsername(), user.getPassword(), user.getEmail() };// 獲得傳遞的參數
 try {
 int result = runner.update(conn, sql, params);// 保存用戶
 if (result > 0) {// 如果保存成功返回true
 return true;
 } else {// 如果保存失敗返回false
 return false;
 }
 } catch (SQLException e) {
 e.printStackTrace();
 } finally {
 DbUtils.closeQuietly(conn);// 關閉連接
 }
 return false;// 如果發生異常返回false
 }
}

 

系統登錄
1.1窗體設計

JAVA實現簡單系統登陸注冊模塊

使用BoxLayout布局,將控件排列方式設置從上至下:

 

 

復制代碼 代碼如下:
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));

 

窗體使用了標簽、文本域、密碼域和按鈕等控件
實現代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class login extends JFrame{
private static final long serialVersionUID = -4655235896173916415L;
private JPanel contentPane;
private JTextField usernameTextField;
private JPasswordField passwordField;
private JTextField validateTextField;
private String randomText;
public static void main(String args[]){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
login frame=new login();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
 
}
public login(){
setTitle("系統登錄");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
 
JPanel usernamePanel=new JPanel();
contentPane.add(usernamePanel);
 
JLabel usernameLable=new JLabel("\u7528\u6237\u540D\uFF1A");
usernameLable.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameLable);
 
usernameTextField=new JTextField();
usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameTextField);
usernameTextField.setColumns(10);
 
JPanel passwordPanel = new JPanel();
contentPane.add(passwordPanel);
JLabel passwordLabel = new JLabel("\u5BC6 \u7801\uFF1A");
passwordLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordField);
JPanel validatePanel = new JPanel();
contentPane.add(validatePanel);
JLabel validateLabel = new JLabel("\u9A8C\u8BC1\u7801\uFF1A");
validateLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateLabel);
validateTextField = new JTextField();
validateTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateTextField);
validateTextField.setColumns(5);
randomText = RandomStringUtils.randomAlphanumeric(4);
CAPTCHALabel label = new CAPTCHALabel(randomText);//隨機驗證碼
label.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(label);
 
JPanel buttonPanel=new JPanel();
contentPane.add(buttonPanel);
 
JButton submitButton=new JButton("登錄");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_submitButton_actionPerformed(e);
}
});
submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(submitButton);
 
JButton cancelButton=new JButton("退出");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_cancelButton_actionPerformed(e);
}
});
cancelButton.setFont(new Font("微軟雅黑",Font.PLAIN,15));
buttonPanel.add(cancelButton);
 
pack();// 自動調整窗體大小
setLocation(com.lixiyu.util.SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示
 
}

窗體居中顯示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class SwingUtil {
/*
* 根據容器的大小,計算居中顯示時左上角坐標
*
* @return 容器左上角坐標
*/
public static Point centreContainer(Dimension size) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 獲得屏幕大小
int x = (screenSize.width - size.width) / 2;// 計算左上角的x坐標
int y = (screenSize.height - size.height) / 2;// 計算左上角的y坐標
return new Point(x, y);// 返回左上角坐標
}
}

1.2獲取及繪制驗證碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class CAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用于保存生成驗證圖片的字符串
public CAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
g.drawString(text, 5, 25);// 繪制字符串
}
}

 

*彩色驗證碼:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ColorfulCAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用于保存生成驗證圖片的字符串
private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };// 定義畫筆顏色數組
public ColorfulCAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
for (int i = 0; i < text.length(); i++) {
g.setColor(colors[RandomUtils.nextInt(colors.length)]);
g.drawString("" + text.charAt(i), 5 + i * 13, 25);// 繪制字符串
}
}
}

1.3非空校驗

?
1
2
3
4
5
6
7
8
9
10
11
12
if (username.isEmpty()) {// 判斷用戶名是否為空
JOptionPane.showMessageDialog(this, "用戶名不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (new String(password).isEmpty()) {// 判斷密碼是否為空
JOptionPane.showMessageDialog(this, "密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (validate.isEmpty()) {// 判斷驗證碼是否為空
JOptionPane.showMessageDialog(this, "驗證碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}

 

1.4合法性校驗

?
1
2
3
4
5
6
7
8
9
10
11
12
if (!DBHelper.exists(username)) {// 如果用戶名不存在則進行提示
JOptionPane.showMessageDialog(this, "用戶名不存在!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (!DBHelper.check(username, password)) {// 如果密碼錯誤則進行提示
JOptionPane.showMessageDialog(this, "密碼錯誤!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (!validate.equals(randomText)) {// 如果校驗碼不匹配則進行提示
JOptionPane.showMessageDialog(this, "驗證碼錯誤!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}

1.5顯示主窗體

?
1
2
3
4
5
6
7
8
9
10
11
12
13
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
MainFrame frame = new MainFrame();// 創建主窗體
frame.setVisible(true);// 設置主窗體可見
} catch (Exception e) {
e.printStackTrace();
}
}
});
dispose();// 將登錄窗體銷毀
}

設計主窗體(比較簡單這個):

 

?
1
2
3
4
5
6
7
8
9
10
11
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設置單擊關閉窗體按鈕時執行的操作
setSize(450, 300);// 設置窗體大小
contentPane = new JPanel();// 創建面板
contentPane.setLayout(new BorderLayout(0, 0));// 設置面板布局使用邊界布局
setContentPane(contentPane);// 應用面板
JLabel tipLabel = new JLabel("恭喜您成功登錄系統!");// 創建標簽
tipLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 40));// 設置標簽字體
contentPane.add(tipLabel, BorderLayout.CENTER);// 應用標簽
setLocation(SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示
}

用戶注冊

1.1窗體設計

JAVA實現簡單系統登陸注冊模塊

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class Register extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2491294229716316338L;
private JPanel contentPane;
private JTextField usernameTextField;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
private JTextField emailTextField;
private JLabel tipLabel = new JLabel();// 顯示提示信息
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setTitle("\u7528\u6237\u6CE8\u518C");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JPanel usernamePanel = new JPanel();
contentPane.add(usernamePanel);
JLabel usernameLabel = new JLabel("\u7528 \u6237 \u540D\uFF1A");
usernameLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameLabel);
usernameTextField = new JTextField();
usernameTextField.setToolTipText("\u8BF7\u8F93\u51655~15\u4E2A\u7531\u5B57\u6BCD\u6570\u5B57\u4E0B\u5212\u7EBF\u7EC4\u6210\u7684\u5B57\u7B26\u4E32");
AbstractDocument doc = (AbstractDocument) usernameTextField.getDocument();
doc.setDocumentFilter(new DocumentSizeFilter(15));// 限制文本域內可以輸入字符長度為15
doc.addDocumentListener(new DocumentSizeListener(tipLabel, 15));
usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameTextField);
usernameTextField.setColumns(10);
JPanel passwordPanel1 = new JPanel();
contentPane.add(passwordPanel1);
JLabel passwordLabel1 = new JLabel("\u8F93\u5165\u5BC6\u7801\uFF1A");
passwordLabel1.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel1.add(passwordLabel1);
passwordField1 = new JPasswordField();
doc = (AbstractDocument) passwordField1.getDocument();
doc.setDocumentFilter(new DocumentSizeFilter(20));// 限制密碼域內可以輸入字符長度為20
doc.addDocumentListener(new DocumentSizeListener(tipLabel, 20));
passwordField1.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordField1.setColumns(10);
passwordPanel1.add(passwordField1);
JPanel passwordPanel2 = new JPanel();
contentPane.add(passwordPanel2);
JLabel passwordLabel2 = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
passwordLabel2.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel2.add(passwordLabel2);
passwordField2 = new JPasswordField();
doc = (AbstractDocument) passwordField2.getDocument();
doc.setDocumentFilter(new DocumentSizeFilter(20));// 限制密碼域內可以輸入字符長度為20
doc.addDocumentListener(new DocumentSizeListener(tipLabel, 20));
passwordField2.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordField2.setColumns(10);
passwordPanel2.add(passwordField2);
JPanel emailPanel = new JPanel();
contentPane.add(emailPanel);
JLabel emailLabel = new JLabel("\u7535\u5B50\u90AE\u7BB1\uFF1A");
emailLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
emailPanel.add(emailLabel);
emailTextField = new JTextField();
doc = (AbstractDocument) emailTextField.getDocument();
doc.setDocumentFilter(new DocumentSizeFilter(45));// 限制文本域內可以輸入字符長度為45
doc.addDocumentListener(new DocumentSizeListener(tipLabel, 45));
emailTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
emailPanel.add(emailTextField);
emailTextField.setColumns(10);
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel);
JButton submitButton = new JButton("\u63D0\u4EA4");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_submitButton_actionPerformed(e);
}
});
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
tipLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(tipLabel);
Component glue = Box.createGlue();
buttonPanel.add(glue);
submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(submitButton);
JButton cancelButton = new JButton("\u53D6\u6D88");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_cancelButton_actionPerformed(e);
}
});
cancelButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(cancelButton);
pack();// 自動調整窗體大小
setLocation(SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示
}

1.2用DocumentFilter限制文本可用字符數

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class DocumentSizeFilter extends DocumentFilter {
private int maxSize;// 獲得文本的最大長度
public DocumentSizeFilter(int maxSize) {
this.maxSize = maxSize;// 獲得用戶輸入的最大長度
}
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if ((fb.getDocument().getLength() + string.length()) <= maxSize) {// 如果插入操作完成后小于最大長度
super.insertString(fb, offset, string, attr);// 調用父類中的方法
} else {
Toolkit.getDefaultToolkit().beep();// 發出提示聲音
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if ((fb.getDocument().getLength() + text.length() - length) <= maxSize) {// 如果替換操作完成后小于最大長度
super.replace(fb, offset, length, text, attrs);// 調用父類中的方法
} else {
Toolkit.getDefaultToolkit().beep();// 發出提示聲音
}
}
}

1.3用DocumentListener接口實現顯示文本控件已用字符

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class DocumentSizeListener implements DocumentListener {
private JLabel tipLabel;
private int maxSize;
public DocumentSizeListener(JLabel tipLabel, int maxSize) {
this.tipLabel = tipLabel;
this.maxSize = maxSize;
}
@Override
public void insertUpdate(DocumentEvent e) {
setTipText(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
setTipText(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
setTipText(e);
}
private void setTipText(DocumentEvent e) {
Document doc = e.getDocument();// 獲得文檔對象
tipLabel.setForeground(Color.BLACK);// 設置字體顏色
if (doc.getLength() > (maxSize * 4 / 5)) {// 如果已輸入字符長度大于最大長度的80%
tipLabel.setForeground(Color.RED);// 使用紅色顯示提示信息
} else {
tipLabel.setForeground(Color.BLACK);// 使用黑色顯示提示信息
}
tipLabel.setText("提示信息:" + doc.getLength() + "/" + maxSize);
}
}

1.4非空校驗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (username.isEmpty()) {// 判斷用戶名是否為空
JOptionPane.showMessageDialog(this, "用戶名不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (new String(password1).isEmpty()) {// 判斷密碼是否為空
JOptionPane.showMessageDialog(this, "密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (new String(password2).isEmpty()) {// 判斷確認密碼是否為空
JOptionPane.showMessageDialog(this, "確認密碼不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (email.isEmpty()) {// 判斷電子郵箱是否為空
JOptionPane.showMessageDialog(this, "電子郵箱不能為空!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}

 

1.5使用正則表達式校驗字符串(合法性校驗)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 校驗用戶名是否合法
if (!Pattern.matches("\\w{5,15}", username)) {
JOptionPane.showMessageDialog(this, "請輸入合法的用戶名!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
// 校驗兩次輸入的密碼是否相同
if (!Arrays.equals(password1, password2)) {
JOptionPane.showMessageDialog(this, "兩次輸入的密碼不同!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
// 校驗電子郵箱是否合法
if (!Pattern.matches("\\w+@\\w+\\.\\w+", email)) {
JOptionPane.showMessageDialog(this, "請輸入合法的電子郵箱!", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
// 校驗用戶名是否存在
if (DBHelper.exists(username)) {
JOptionPane.showMessageDialog(this, "用戶名已經存在", "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}

1.6保存注冊信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
User user = new User();
user.setUsername(username);
user.setPassword(new String(password1));
user.setEmail(email);
Arrays.fill(password1, '0');// 清空保存密碼的字符數組
Arrays.fill(password2, '0');// 清空保存密碼的字符數組
if (DBHelper.save(user)) {
JOptionPane.showMessageDialog(this, "用戶注冊成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
JOptionPane.showMessageDialog(this, "用戶注冊失?。?quot;, "警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
}

至此,一個簡單而有完整的登陸注冊模塊就完成了。

以上就是本文的全部內容,希望大家可以喜歡。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 云南一级毛片 | 免费在线观看黄色av | 日韩和的一区二在线 | 国产精品久久久久一区二区三区 | 欧美精品一二三区 | 一级片在线观看 | 精品无码久久久久久国产 | 久久国产精品久久 | 日本二区视频 | 91精品国产91久久综合桃花 | 久久99精品久久久久久国产越南 | 中文字幕一二三 | 中文字幕免费看 | 欧美在线视屏 | 国产精品免费一区二区三区四区 | 中文字幕av黄色 | 一区二区三区国产在线观看 | 欧美日韩国产影院 | 操少妇逼视频 | 黄视频免费观看 | 亚洲一区国产精品 | 精品国产乱码久久久久夜 | 波多野吉衣网站 | 国产黄色小视频 | 精品福利一区二区三区 | 综合五月 | 欧美日韩国产精品一区 | 一级片在线观看 | 一区二区三区 在线 | 米奇777超碰欧美日韩亚洲 | 欧美一级在线 | 欧美第一区 | 国产伦精品一区二区三区四区视频 | 国产中文字幕一区 | 五月激情综合 | 天天噜天天干 | 中文字幕一区二区三区乱码在线 | 四虎影视免费看电影 | 91精品国产综合久久香蕉922 | 日本在线一区二区 | 成人午夜视频在线观看 |