登录窗口java

这次代码是登录窗口的制作。

主要的方面是是包括,用户名、密码、验证码。以及输入数据所需要的文本框,对于验证码可以通过点击验证码进行修改。同时对于验证码的前景色和背景色同时都得到修改。

点击注册(这里还不完善)系统会提示你登录时需要输入的字符串,之后按照提示的字符串进行输入之后烯烃会对用户名以及密码验证码进行判断。可以通过enter直接进行登录二不用点击相应的按钮,之后如果错误会清空所输入的东西让您继续输入。 输入成功后即完成简单的登录。

重要内容是验证码的随机产生并进行显示。可以控制验证码的颜色以及字体还有大小。

package com.登录窗口;
    //模拟登录窗口
    import java.util.Random;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class DengLu  extends JFrame 
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;//系统进行的添加
        static String wen=new String();
        private JLabel yonghula;
        private JLabel mimala;
        private JLabel yanzhengla;
        private JTextField yonghutxt;
        private JPasswordField mimatxt;
        private JTextField yanzhengtxt;
        private JButton anniu1bt;
        private JButton anniu2bt;
        private JButton mianban;
         public Color getRandomColor()
         {//获得随机颜色
             Random r=new Random();
             int R=r.nextInt(255),G=r.nextInt(255),B=r.nextInt(255);
              return new Color(R,G,B);
         }
        public String random()
        {
            Random r=new Random();
                String result = "";//随机产生四个数字或者是字母
                for(int i=0;i<4;i++){
                    int shuzi;
                    //生成一个1~3的int型的整数
                    shuzi=(r.nextInt(3)+1);
                     if(shuzi==1){//数字
                        shuzi=(r.nextInt(8)+1);
                        result=result+shuzi;
                    }
                    else if(shuzi==2){//大写字母
                        shuzi=(r.nextInt(25)+97);
                        result=result+(char)shuzi;
                    }
                    else {//小写字母
                        shuzi=(r.nextInt(25)+65);
                        result=result+(char)shuzi;
                    }
                }
                return result;
        }
        public DengLu()
        {
            this.setTitle("登录窗口界面");
            //用户的文本
            yonghula=new JLabel();
            yonghula.setText("用户名:");
            yonghula.setSize(60,50);
            yonghula.setLocation(100,80);
            //用户的输入框
            yonghutxt=new JTextField();
            yonghutxt.setSize(120,28);
            yonghutxt.setLocation(150,95);
            //密码文本
            mimala=new JLabel();
            mimala.setText("密   码:");
            mimala.setSize(50,50);
            mimala.setLocation(100,120);
            //密码输入框
            mimatxt=new JPasswordField();
            mimatxt.setEchoChar('$');
            mimatxt.setSize(120,28);
            mimatxt.setLocation(150,135);
            //登录按钮
            anniu1bt=new JButton("登录");
            anniu1bt.setSize(60,25);
            anniu1bt.setLocation(140,220);
            //注册按钮
            anniu2bt = new JButton("注册");
            anniu2bt.setSize(60, 25);
            anniu2bt.setLocation(240, 220);
            //验证码文本
            yanzhengla=new JLabel();
            yanzhengla.setText("验证码:");
            yanzhengla.setSize(70, 50);
            yanzhengla.setLocation(100,165);
            //验证码文本框
            yanzhengtxt= new JTextField();
            yanzhengtxt.setSize(50, 28);
            yanzhengtxt.setLocation(150, 180);
            //验证码
            mianban = new JButton();
            mianban.setSize(80, 30);
            mianban.setLocation(210, 180);
            wen=random();
            mianban.setText(wen);
            mianban.setFont(new Font("华文行楷",Font.BOLD,15));
            //判断
            anniu1bt.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if(yonghutxt.getText().trim().equals("dazhi")&&new String(mimatxt.getPassword()).trim().equals("dazhi666")&&yanzhengtxt.getText().trim().equals(wen))
                    {
                        JOptionPane.showMessageDialog(null, "登陆成功!");
                        System.exit(-1);
                    }
                    else if(yonghutxt.getText().trim().equals(""))
                        JOptionPane.showMessageDialog(null, "用户名不能为空");
                    else if(new String(mimatxt.getPassword()).trim().equals(""))
                        JOptionPane.showMessageDialog(null, "密码不能为空");
                    else if(yanzhengtxt.getText().trim().equals(""))
                        JOptionPane.showMessageDialog(null, "验证码不能为空");
                    else
                        JOptionPane.showMessageDialog(null, "登录错误");
                    yonghutxt.setText("");
                    mimatxt.setText("");
                    yanzhengtxt.setText("");
                    mianban.setBackground(getRandomColor());
                    mianban.setForeground(getRandomColor());
                    Random r=new Random();
                       String result = "";//随机产生四个数字或者是字母
                       for(int i=0;i<4;i++){
                           int shuzi;
                           //生成一个1~3的int型的整数
                           shuzi=(r.nextInt(3)+1);
                            if(shuzi==1){//数字
                               shuzi=(r.nextInt(8)+1);
                               result=result+shuzi;
                           }
                           else if(shuzi==2){//大写字母
                               shuzi=(r.nextInt(25)+97);
                               result=result+(char)shuzi;
                           }
                           else {//小写字母
                               shuzi=(r.nextInt(25)+65);
                               result=result+(char)shuzi;
                           }
                       }
                    wen=result;
                    mianban.setText(wen);
                }
            });
            //验证码处理
            anniu2bt.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    // TODO 自动生成的方法存根
                    JOptionPane.showMessageDialog(null, "注册成功\n用户名:dazhi\n密码:dazhi666");
                       yonghutxt.setText("");
                       mimatxt.setText("");
                       yanzhengtxt.setText("");
                       Random r=new Random();
                       String result = "";//随机产生四个数字或者是字母
                       for(int i=0;i<4;i++){
                           int shuzi;
                           //生成一个1~3的int型的整数
                           shuzi=(r.nextInt(3)+1);
                        if(shuzi==1){//数字
                               shuzi=(r.nextInt(8)+1);
                               result=result+shuzi;
                           }
                           else if(shuzi==2){//大写字母
                               shuzi=(r.nextInt(25)+97);
                               result=result+(char)shuzi;
                           }
                           else {//小写字母
                               shuzi=(r.nextInt(25)+65);
                               result=result+(char)shuzi;
                           }
                       }
                       wen=result;
                       mianban.setText(wen);
                }
                });
            //重新获取验证码
           mianban.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    mianban.setBackground(getRandomColor());
                    mianban.setForeground(getRandomColor());
                    Random r=new Random();
                       String result = "";//随机产生四个数字或者是字母(大写后者小写)
                       for(int i=0;i<4;i++){
                           int shuzi;
                           //生成一个1~3的int型的整数
                           shuzi=(r.nextInt(3)+1);
                            if(shuzi==1){//数字
                               shuzi=(r.nextInt(8)+1);
                               result=result+shuzi;
                           }
                           else if(shuzi==2){//大写字母
                               shuzi=(r.nextInt(25)+97);
                               result=result+(char)shuzi;
                           }
                           else {//小写字母
                               shuzi=(r.nextInt(25)+65);
                               result=result+(char)shuzi;
                           }
                       }
                    wen=result;
                    mianban.setText(wen);
                }
            });
         this.setLayout(null);//告知管理器这里不用布局管理器
         this.getRootPane().setDefaultButton(anniu1bt);//按回车需要执行的按钮操作
         this.setSize(400, 400);
         this.add(yonghula);
         this.add(yonghutxt);
         this.add(mimala);
         this.add(mimatxt);
         this.add(mimala);
         this.add(yanzhengla);
         this.add(yanzhengtxt);
         this.add(anniu1bt);
         this.add(anniu2bt);
         this.add(mianban);
         this.setVisible(true);//使窗口可视
         this.setResizable(true);//生成的窗口是否可以自由改变大小
         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) 
        {
            new DengLu();
        }
    }
        

 

posted @ 2019-10-14 20:27  喜欢爬的孩子  阅读(1013)  评论(0)    收藏  举报