JAVA語言實現簡單登入介面

來源:互聯網
上載者:User

標籤:blog   結果   exit   style   實現   print   輸入   insert   enter   

  1. 程式設計思想:
      使用Math.random()方法迴圈產生6個97~122之間的隨機整數(對應ASCII碼值‘a’~‘z’),將其轉化為char型變數,串連成為一個6位字串作為驗證碼輸出,提示使用者輸入。使用者輸入字串與驗證碼相符則提示通過,不相符則提示錯誤並再次產生隨機驗證碼。
  2. 程式流程圖:
  3. 來源程式:
      1 import java.awt.EventQueue;  2 import javax.swing.JFrame;  3 import java.awt.Color;  4 import javax.swing.JPanel;  5 import java.awt.BorderLayout;  6 import javax.swing.JLabel;  7 import javax.swing.JTextField;  8 import javax.swing.JPasswordField;  9 import javax.swing.JButton; 10 import javax.swing.event.*; 11 import java.awt.Font; 12 public class LoginWindow { 13  14     private JFrame frame; 15     private JPasswordField passwordField; 16  17     /** 18      * Launch the application. 19      */ 20     public static void main(String[] args) { 21         EventQueue.invokeLater(new Runnable() { 22             public void run() { 23                 try { 24                     LoginWindow window = new LoginWindow(); 25                     window.frame.setVisible(true); 26                 } catch (Exception e) { 27                     e.printStackTrace(); 28                 } 29             } 30         }); 31     } 32  33     /** 34      * Create the application. 35      */ 36     public LoginWindow() { 37         initialize(); 38     } 39  40     /** 41      * Initialize the contents of the frame. 42      */ 43     private void initialize() { 44         frame = new JFrame(); 45         frame.setBackground(Color.BLUE); 46         frame.setBounds(100, 100, 445, 319); 47         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 48          49         JPanel panel = new JPanel(); 50         frame.getContentPane().add(panel, BorderLayout.CENTER); 51         panel.setVisible(true); 52         panel.setLayout(null); 53          54         JLabel label = new JLabel("\u767B\u5F55\u540D\uFF1A"); 55         label.setFont(new Font("微軟雅黑", Font.PLAIN, 18)); 56         label.setBounds(85, 44, 76, 32); 57         panel.add(label); 58          59         JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A"); 60         label_1.setFont(new Font("微軟雅黑", Font.PLAIN, 18)); 61         label_1.setBounds(85, 86, 76, 32); 62         panel.add(label_1); 63          64         JLabel label_2 = new JLabel("\u9A8C\u8BC1\u7801\uFF1A"); 65         label_2.setFont(new Font("微軟雅黑", Font.PLAIN, 18)); 66         label_2.setBounds(84, 128, 76, 30); 67         panel.add(label_2); 68          69         JTextField textArea = new JTextField(); 70         textArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255,0,0))); 71         textArea.setBounds(181, 44, 166, 34); 72         panel.add(textArea); 73          74         passwordField = new JPasswordField(); 75         passwordField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255,0,0))); 76         passwordField.setBounds(181, 86, 166, 34); 77         panel.add(passwordField); 78          79         JButton btnNewButton = new JButton("\u767B\u5F55"); 80         btnNewButton.setEnabled(false); 81         btnNewButton.setBackground(new Color(51, 153, 0)); 82         btnNewButton.setForeground(Color.BLACK); 83         btnNewButton.setBounds(124, 186, 181, 29); 84         panel.add(btnNewButton); 85          86         JButton btnNewButton_1 = new JButton("\u5FEB\u901F\u6CE8\u518C"); 87         btnNewButton_1.setBackground(new Color(255, 204, 255)); 88         btnNewButton_1.setBounds(124, 225, 181, 29); 89         panel.add(btnNewButton_1); 90          91          92         String result = ""; 93         for(int i = 0;i < 6;i++) 94         { 95             int c = (int)(Math.random() * 26 + 97); 96             result = result + (char)c; 97         } 98         JLabel lblValid = new JLabel(result); 99         lblValid.setBounds(277, 134, 70, 22);100         panel.add(lblValid);101         102         JTextField textArea_1 = new JTextField();103         textArea_1.getDocument().addDocumentListener(new DocumentListener(){104             public void insertUpdate(DocumentEvent e) {105                 if(textArea_1.getText().equals(lblValid.getText())){106                     btnNewButton.setEnabled(true);107                 }108                 else{109                     btnNewButton.setEnabled(false);110                 }111             }112             public void removeUpdate(DocumentEvent e) {113                 if(textArea_1.getText().equals(lblValid.getText())){114                     btnNewButton.setEnabled(true);115                 }116                 else{117                     btnNewButton.setEnabled(false);118                 }119             }120             public void changedUpdate(DocumentEvent e) {121                 122             }123         });124         125         textArea_1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255,0,0)));126         textArea_1.setBounds(180, 128, 76, 32);127         panel.add(textArea_1);128     }129 }

     

  4. 實現結果圖:
         

     

  5. 實驗總結:

    參考資料:

    http://www.yiibai.com/swing/home.html

    https://zhidao.baidu.com/question/554112250.html

JAVA語言實現簡單登入介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.