Java登陸驗證碼問題

來源:互聯網
上載者:User

標籤:http   登陸   rdf   自動產生   產生   sys   rand   運行   col   

程式設計思路

隨機產生一個6位的字串
讀取使用者輸入的資訊,包括帳號密碼。
驗證使用者輸入的驗證碼是不是和產生的隨機字串一樣。
如果不是一樣的話就提示並且回到重新輸入的介面。
如果一樣的話就提示登陸成功,並且重新整理驗證碼,以方便下一個使用者的使用。

程式流程圖

程式原始碼
import java.awt.Color;import java.awt.Component;public static void main(String[] args) {    // TODO 自動產生的方法存根JFrame frame=new JFrame("登陸測試");frame.setBounds(500, 300, 350, 200);frame.setSize(350,200);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   Panel panel=new Panel();frame.add(panel);placeCompoents(panel);panel.setBackground(Color.LIGHT_GRAY);frame.setVisible(true);}private static void placeCompoents(Panel panel) {    // TODO 自動產生的方法存根panel.setLayout(null);//姓名資訊JLabel userLabel=new JLabel("User");userLabel.setBounds(10, 20, 80, 25);panel.add(userLabel);JTextField userText=new JTextField(20);userText.setBounds(100, 20, 165, 25);panel.add(userText);//密碼資訊JLabel passwordLabel=new JLabel("password");passwordLabel.setBounds(10, 50, 80, 25);panel.add(passwordLabel);JPasswordField passwordText=new JPasswordField(20);passwordText.setBounds(100, 50, 165, 25);panel.add(passwordText);//驗證碼資訊String code="";for(int i=0;i<6;i++) {          int intVal=(int)(Math.random()*26+97);    code=code+(char)intVal; }String codex=code;JLabel ident=new JLabel(code);ident.setBounds(200, 80, 80, 25);panel.add(ident);// 驗證碼文本域JLabel codes=new JLabel("codes");codes.setBounds(10, 80, 80, 25);panel.add(codes);JTextField codeText=new JTextField(6);codeText.setBounds(100, 80, 80, 25);panel.add(codeText);//按鈕JButton loginButton=new JButton("login");loginButton.setBounds(100, 100, 80, 25);panel.add(loginButton);loginButton.addActionListener(new ActionListener() {    public void actionPerformed(ActionEvent e) {    String c=codeText.getText();    String u=userText.getText();    char[] p1=passwordText.getPassword();    String p="";    p=p.valueOf(p1);    if(c.equals(codex))         {         JOptionPane.showMessageDialog(null,"hello "+u+",歡迎使用!祝您一天愉快");    //           userText.setText("你媽媽喊你回家吃飯");         passwordText.setText("");         codeText.setText("");         String code="";            for(int i=0;i<6;i++) {                      int intVal=(int)(Math.random()*26+97);                code=code+(char)intVal;             }            String codex=code;            ident.setText(codex);         }     else         {         JOptionPane.showMessageDialog(null,"驗證碼錯誤");                }          }       }   );JButton exit=new JButton("exit");exit.addActionListener(new ActionListener() {  public void actionPerformed(ActionEvent e) {     System.exit(0);  }}     );exit.setBounds(200, 100, 80, 25);panel.add(exit);}}
程式運行

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.