上次也寫了一個微博登陸頁面,不過功能還不夠完善。今天重新完善了一些功能,分享出來給大家。
基本功能如下:
(1)具有類似新浪微博的用戶注冊圖形界面.
(2)使用用戶名或手機號注冊,注冊時需要提供新密碼和
確認密碼。
(3)注冊時,用戶名重復和手機號重復時能提供提示。
(4)注冊時,手機號明顯錯誤時能提供提示,手機號長 度為11
位,並且以 13、15、17、18 開頭的號碼判斷為正確。
(5)程序以應用程序形式實現,不用 web 網頁形式實現。
(6)用戶數據存儲以 HashSet 集合實現。
1 package com.bloge; 2 public class Weibo { 3 public static void main(String[] ars) { 4 WindowButton win = new WindowButton(); 5 win.setBounds(400, 150, 606, 428); 6 win.setTitle("微博"); 7 } 8 }
有一些圖片是我截圖弄來的。如果想顯示背景圖片和小圖標的話,那就自己找圖片放上去就可以了。
1 package com.bloge; 2 3 import java.util.*; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.awt.Font;//字體 8 import java.awt.Color;//顏色 9 import java.util.HashSet; 10 11 public class WindowButton extends JFrame implements ActionListener, 12 FocusListener { 13 14 Box boxV1, boxV2, boxV3, baseBox;// boxV1,boxV2,boxV3均為列式,baseBox為行式 15 JButton button; 16 JMenuBar menubar; 17 JMenu menu1, menu2; 18 JTextField text1; // 手機號的文本域 19 JPasswordField pass1, pass2; // 密碼和確認密碼的文本域 20 JComboBox<String> comBox; 21 22 // labePhone為手機標簽,labe1,labe2,labe3分別為手機、密碼、確認密碼的提示標簽 23 JLabel labePhone, labe1, labe2, labe3; 24 int flag = 0, flag2 = 1; // 標記符 25 HashSet<String> set1 = new HashSet<String>(); // HashSet集合,用於儲存號碼 26 HashSet<String> set2 = new HashSet<String>(); // HashSet集合,用於儲存密碼 27 28 WindowButton() { 29 addWindowListener(new WindowAdapter() { // 匿名類的實例監視窗口事件 30 public void windowClosing(WindowEvent e) { 31 dispose(); 32 } 33 }); 34 35 setLayout(new FlowLayout()); 36 setVisible(true); 37 38 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 39 setResizable(false); // 設置框架不可以改變大小 40 41 menubar = new JMenuBar(); 42 menu1 = new JMenu("個人注冊"); 43 menu2 = new JMenu("郵箱注冊"); 44 menubar.add(menu1); // 向菜單條里添加菜單 45 menubar.add(menu2); 46 setJMenuBar(menubar); 47 48 Toolkit tk = Toolkit.getDefaultToolkit(); // 設置窗口圖標 49 Image imge = tk.getImage("src\\Img\\20.png"); 50 setIconImage(imge); 51 52 Icon icon1 = new ImageIcon("src\\Img\\520.png"); // 手機圖標的圖片 53 54 boxV1 = Box.createVerticalBox(); // 列型盒式布局 55 boxV1.add(new JLabel("來自")); 56 boxV1.add(Box.createVerticalStrut(15)); 57 labePhone = new JLabel("手機"); 58 labePhone.setIcon(icon1); 59 60 labePhone.setFont(new Font("宋體", 1, 12));// 設置標簽字體 61 // labePhone.setForeground(Color.yellow); 62 63 boxV1.add(labePhone); 64 // 垂直支撐,可以得到一個不可見的垂直Strut對象 65 boxV1.add(Box.createVerticalStrut(15)); 66 67 boxV1.add(new JLabel("設置密碼")); 68 boxV1.add(Box.createVerticalStrut(15)); 69 boxV1.add(new JLabel("確認密碼")); 70 boxV2 = Box.createVerticalBox(); // 列型盒式布局 71 boxV2.add(Box.createVerticalStrut(35)); 72 73 comBox = new JComboBox<String>(); 74 comBox.addItem("中國"); 75 comBox.addItem("美國"); 76 comBox.addItem("加拿大"); 77 comBox.addItem("巴西"); 78 comBox.addItem("馬來西亞"); 79 comBox.addItem("日本"); 80 comBox.addItem("韓國"); 81 comBox.addItem("英國"); 82 comBox.addItem("法國"); 83 comBox.addItem("印度"); 84 comBox.addItem("德國"); 85 comBox.addItem("越南"); 86 add(comBox); 87 boxV2.add(comBox); 88 89 text1 = new JTextField(10); // 輸入手機號的文本域 90 pass1 = new JPasswordField(); 91 pass2 = new JPasswordField(); 92 93 boxV2.add(Box.createVerticalStrut(10)); 94 boxV2.add(text1); // 第一行,手機 95 96 text1.addFocusListener(this); // 為手機注冊焦點事件,手機號 97 pass1.addFocusListener(this); 98 pass2.addFocusListener(this); 99 100 boxV2.add(Box.createVerticalStrut(10)); 101 boxV2.add(pass1); 102 boxV2.add(Box.createVerticalStrut(10)); 103 boxV2.add(pass2); 104 105 boxV2.add(Box.createVerticalStrut(10)); 106 button = new JButton("立即注冊"); 107 boxV2.add(button); 108 109 button.addActionListener(this); // 注冊監聽器,button為事件源,this為監聽器 110 111 labe1 = new JLabel(""); 112 labe2 = new JLabel(""); 113 labe3 = new JLabel(""); 114 115 boxV3 = Box.createVerticalBox(); // 列型盒式布局 116 boxV3.add(Box.createVerticalStrut(35)); 117 boxV3.add(labe1); 118 boxV3.add(Box.createVerticalStrut(15)); 119 boxV3.add(labe2); 120 boxV3.add(Box.createVerticalStrut(15)); 121 boxV3.add(labe3); 122 123 baseBox = Box.createHorizontalBox(); // 列型盒式布局 124 baseBox.add(boxV1); 125 baseBox.add(Box.createHorizontalStrut(18)); 126 baseBox.add(boxV2); 127 baseBox.add(Box.createHorizontalStrut(18)); 128 baseBox.add(boxV3); 129 add(baseBox); 130 131 labe1.setText(" " 132 + " "); // 手機提示標簽 133 134 ImageIcon img = new ImageIcon("src\\Img\\gg.jpg");// 設置背景圖片 135 136 JLabel imgLabel = new JLabel(img);// 將背景圖放在標簽里。 137 // 將背景標簽添加到jfram的LayeredPane面板里。 138 getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE)); 139 // 設置背景標簽的位置 140 imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); 141 142 Container contain = this.getContentPane(); 143 // 將內容面板設為透明。將LayeredPane面板中的背景顯示出來。 144 ((JPanel) contain).setOpaque(false); 145 146 validate(); // 刷新 147 } 148 149 public void focusGained(FocusEvent e) { // 無輸入焦點變為有輸入焦點 150 if (e.getSource() == text1) { 151 152 Icon icon2 = new ImageIcon("src\\Img\\h1.png"); 153 labe1.setText("請輸入手機號! "); 154 labe1.setFont(new Font("宋體", 0, 12));// 設置標簽字體 155 labe1.setForeground(Color.blue); 156 labe1.setIcon(icon2); 157 } 158 if (e.getSource() == pass1) { 159 // labe2.setText(" "); 160 Icon icon2 = new ImageIcon("src\\Img\\h1.png"); 161 labe2.setText("請輸入密碼!"); 162 labe2.setFont(new Font("宋體", 0, 12));// 設置標簽字體 163 labe2.setForeground(Color.blue); 164 labe2.setIcon(icon2); 165 } 166 if (e.getSource() == pass2) { 167 // labe3.setText(" "); 168 Icon icon2 = new ImageIcon("src\\Img\\h1.png"); 169 labe3.setText("請確認密碼!"); 170 labe3.setFont(new Font("宋體", 0, 12));// 設置標簽字體 171 labe3.setForeground(Color.blue); 172 labe3.setIcon(icon2); 173 } 174 175 } 176 177 public void focusLost(FocusEvent e) { // 有輸入焦點變為無輸入焦點 178 179 try { 180 181 if (e.getSource() == text1) { 182 // labe1.setText("請輸入密碼!"); 183 String str = String.valueOf(text1.getText()); // 將數字轉化為字符串 184 String[] str1 = new String[str.length()]; 185 186 for (int i = 0; i < str.length(); i++) { 187 str1[i] = str.substring(i, i + 1); // 截取 188 } 189 190 set1.add("13458351110"); 191 set1.add("17934558544"); 192 set1.add("13435455522"); 193 set1.add("14795823333"); 194 set1.add("15977178388"); 195 196 // 構造迭代器,用iterator()進行遍歷 197 Iterator<String> te = set1.iterator(); 198 while (te.hasNext()) { 199 if (str.equals(te.next())) { 200 str = "8888"; 201 202 } 203 204 } 205 206 if (str1[0].equals("1")) { // 必須用equals比較 207 if ((str1[1].equals("0") || str1[1].equals("1") 208 || str1[1].equals("2") || str1[1].equals("6") || str1[1] 209 .equals("9")) 210 || ((text1.getText().length()) != 11)) { 211 212 Icon icon3 = new ImageIcon("src\\Img\\h2.png"); 213 labe1.setText("請輸入開頭為13,14,15,17,18的號碼,且為11位!"); 214 labe1.setFont(new Font("宋體", 0, 12));// 設置標簽字體 215 labe1.setForeground(Color.red); 216 labe1.setIcon(icon3); 217 flag = 0; 218 } else if (str.equals("8888")) { // text1.getText().equals("1111") 219 JOptionPane.showMessageDialog(null, "該手機號已被注冊!"); // 彈出消息框 220 Icon icon3 = new ImageIcon("src\\Img\\h2.png"); 221 labe1.setText("手機號碼重復! "); 222 labe1.setIcon(icon3); 223 flag = 0; 224 flag2 = 1; 225 } else { 226 Icon icon3 = new ImageIcon("src\\Img\\g1.png"); 227 // 手機號碼正確,只顯示正確的那個圖標 228 labe1.setText(" "); 229 labe1.setIcon(icon3); 230 flag = 1; 231 } 232 } else { 233 Icon icon3 = new ImageIcon("src\\Img\\h2.png"); 234 labe1.setText("請輸入開頭為13,14,15,17,18的號碼,且為11位!"); 235 labe1.setFont(new Font("宋體", 0, 12));// 設置標簽字體 236 labe1.setForeground(Color.red); 237 labe1.setIcon(icon3); 238 flag = 0; 239 } 240 } 241 242 String s = String.valueOf(pass1.getPassword()); // 將數字轉換成字符串 243 if (e.getSource() == pass1) { 244 if ((s.length() < 5 && s.length() > 0) || s.length() > 12) { 245 JOptionPane.showMessageDialog(null, "密碼長度必須在5~12個字符之內!"); 246 247 } else { 248 Icon icon2 = new ImageIcon("src\\Img\\g1.png"); // 第一個密碼成功圖標 249 labe2.setText(" "); 250 labe2.setIcon(icon2); 251 } 252 } 253 254 } catch (Exception ee) { 255 // System.out.println(ee1); 256 } 257 258 } 259 260 public void actionPerformed(ActionEvent e) { 261 if (e.getSource() == button) { 262 263 try { 264 // String str = String.valueOf(text1.getText()); //將數字轉化為字符串 265 String str2 = String.valueOf(pass1.getPassword()); 266 String str3 = String.valueOf(pass2.getPassword()); 267 268 if (!(str2.equals(str3))) { 269 Icon icon2 = new ImageIcon("src\\Img\\h1.png"); 270 labe3.setText("密碼正確!"); 271 labe3.setFont(new Font("宋體", 0, 12));// 設置標簽字體 272 labe3.setForeground(Color.blue); 273 labe3.setIcon(icon2); 274 275 JOptionPane.showMessageDialog(null, "兩次密碼不一致"); 276 } else if (str2.equals(str3) && flag == 1 && flag2 == 1) { 277 JOptionPane.showMessageDialog(null, "注冊成功!"); 278 String string1 = String.valueOf(text1.getText()); 279 String string2 = String.valueOf(pass2.getPassword()); 280 set1.add(string1); // 向集合中添加注冊成功的新號碼 281 set2.add(string2); // 向集合中添加注冊成功的新密碼 282 flag2 = 0; 283 } else { 284 if (flag2 != 0) { 285 JOptionPane.showMessageDialog(null, "請輸入正確的手機號!"); 286 } 287 } 288 } catch (Exception ee1) { 289 } 290 } 291 } 292 }
運行結果