应用场景:用户注册,密码输入

功能界面:

193017785.jpg

窗口界面设计部分代码:

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
public  class  JPasswordFieldTest  extends  JFrame {
                       
     /**
      *
      */
     private  static  final  long  serialVersionUID = 8633179606754193326L;
     private  JPanel contentPane;
     private  JPasswordField passwordField1;
     private  JPasswordField passwordField2;
                       
     /**
      * 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() {
             public  void  run() {
                 try  {
                     JPasswordFieldTest frame =  new  JPasswordFieldTest();
                     frame.setVisible( true );
                 catch  (Exception e) {
                     e.printStackTrace();
                 }
             }
         });
     }
                       
     /**
      * Create the frame.
      */
     public  JPasswordFieldTest() {
         setTitle( "\u5BC6\u7801\u57DF\u63A7\u4EF6\u7B80\u5355\u5E94\u7528" );
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setBounds( 100 100 450 200 );
         contentPane =  new  JPanel();
         contentPane.setBorder( new  EmptyBorder( 5 5 5 5 ));
         setContentPane(contentPane);
         contentPane.setLayout( new  GridLayout( 3 1 5 5 ));
                           
         JPanel passwordPanel1 =  new  JPanel();
         contentPane.add(passwordPanel1);
                           
         JLabel label1 =  new  JLabel( "\u8F93\u5165\u5BC6\u7801\uFF1A" );
         label1.setFont( new  Font( "微软雅黑" , Font.PLAIN,  16 ));
         passwordPanel1.add(label1);
                           
         passwordField1 =  new  JPasswordField();
         passwordField1.setFont( new  Font( "微软雅黑" , Font.PLAIN,  16 ));
         passwordField1.setColumns( 20 );
         passwordPanel1.add(passwordField1);
                           
         JPanel passwordPanel2 =  new  JPanel();
         contentPane.add(passwordPanel2);
                           
         JLabel label2 =  new  JLabel( "\u786E\u8BA4\u5BC6\u7801\uFF1A" );
         label2.setFont( new  Font( "微软雅黑" , Font.PLAIN,  16 ));
         passwordPanel2.add(label2);
                           
         passwordField2 =  new  JPasswordField();
         passwordField2.setFont( new  Font( "微软雅黑" , Font.PLAIN,  16 ));
         passwordField2.setColumns( 20 );
         passwordPanel2.add(passwordField2);
                           
         JPanel buttonPanel =  new  JPanel();
         contentPane.add(buttonPanel);
                           
         JButton submitButton =  new  JButton( "\u63D0\u4EA4" );
         submitButton.addActionListener( new  ActionListener() {
             public  void  actionPerformed(ActionEvent e) {
                 do_submitButton_actionPerformed(e);
             }
         });
         submitButton.setFont( new  Font( "微软雅黑" , Font.PLAIN,  18 ));
         buttonPanel.add(submitButton);
     }


判断部分代码:

1
2
3
4
5
6
7
8
9
10
11
protected  void  do_submitButton_actionPerformed(ActionEvent e) {
        char [] password1 = passwordField1.getPassword();
        char [] password2 = passwordField2.getPassword();
        if  (password1.length <  6 ) {
            JOptionPane.showMessageDialog( this "密码长度小于6位" "" , JOptionPane.WARNING_MESSAGE);
        else  if  (!Arrays.equals(password1, password2)) {
            JOptionPane.showMessageDialog( this "两次密码不同" "" , JOptionPane.WARNING_MESSAGE);
        else  {
            JOptionPane.showMessageDialog( this "两次密码相同" "" , JOptionPane.INFORMATION_MESSAGE);
        }
    }

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1312333,如需转载请自行联系原作者