zoukankan      html  css  js  c++  java
  • JAVA语言实现简单登录界面

    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("u767Bu5F55u540DuFF1A");  //登录名标签
       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("u5BC6u7801uFF1A");  //密码标签
       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("u9A8Cu8BC1u7801uFF1A");    //验证码标签
       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("u767Bu5F55");    //登录按钮
       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("u5FEBu901Fu6CE8u518C");    //快速注册按钮
       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);    //随机生成6个字符
       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. 实验总结:
      • Math.random():产生一个[0,1)之间的随机数。若要产生指定范围[m,n)内随机数,可使用公式Math.random()*(n+1-m) + m。本程序中为产生[97,122]之间随机整数,先使用Math.random()*(122 + 1 - 97) + 97产生一个[97,123)之间的随机数,再进通过类型转换成为整型数据(只保留整数部分)从而得到97~122之间的整数。
      • 通过这次实验,我掌握了Math.random()的用法,了解了Java内类型转换的机制。另外,也初步接触了java的swing组件,熟悉了MyEclipse的设计窗口。
      • 在调试过程中,我遇到的主要问题是如何给文本框添加内容改变事件,通过查找资料找到了正确的方法。相信在以后的学习中会有更深体会。

        参考资料:

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

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

  • 相关阅读:
    二分图匹配【模板】
    高斯消元【模板】
    G. 小花梨的函数
    数字计数
    选课
    二叉苹果树
    重建道路
    【UVA10187】Headmaster's Headache(校长的烦恼)
    【51NOD1447】好记的字符串
    【51NOD1779】逆序对统计
  • 原文地址:https://www.cnblogs.com/lzq666/p/7631327.html
Copyright © 2011-2022 走看看