简单的登录系统界面

代码如下:

package denglujiemian1.text1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;


import java.math.*;
import java.util.Random;
public  class Login1 extends JFrame implements ActionListener{
    private JLabel user,password,check,user0,password0;
    private JTextField word1,word2,word3;
    private JPasswordField word4,word5;
    private Button login,register,checkbutton,yes;
    private JFrame background1,background2;
     static String num;
     static String zhanghao[]=new String[10],mima[]=new String[10];
     static int flag=1;
     static int flag1=1;
     static int flag2=1;
     static int flag3=1;
     static int sum=0;
    

    public static void main(String[] args) {
        Login1 deng= new Login1();
        deng.showTime();
        
    }
    public void showTime()
    {
        //总的框
        background1 = new JFrame();
        background1.setTitle("登录界面");
        background1.setLayout(null);//居中
        
        //账号输入
        user =new JLabel("账号");
        word1=new JTextField("",15);
        //设置“账号”以及输入框的位置
        user.setBounds(70, 13, 60, 40);
        word1.setBounds(110, 20, 210, 25);
        word1.addActionListener(this);
        background1.add(user);
        background1.add(word1);
        //密码输入
        password = new JLabel("密码");
        word4 = new JPasswordField("",15);
        password.setBounds(70, 53, 60, 40);
        word4.setBounds(110, 60, 210, 25);
        background1.add(password);
        background1.add(word4);
        //验证码输入
        check=new JLabel("验证码");
        word2 = new JTextField("",10);
        check.setBounds(58, 93, 60, 40);
        word2.setBounds(110, 100, 100, 25);
        background1.add(check);
        background1.add(word2);
        //验证码数字
        checkButton();
        background1.add(checkbutton);
        
        //设置登录按钮
        login = new Button("登录");
        login.setBackground(Color.orange);
        login.setBounds(80, 150, 240, 40);
        login.addActionListener(this);
        background1.add(login);
        //设置注册按钮
        register = new Button("注册");
        register.setBackground(Color.orange);
        register.setBounds(80, 205, 240, 40);
        register.addActionListener(this);
        background1.add(register);
        
        
        background1.setVisible(true);
        background1.setSize(400, 300);
        background1.setLocationRelativeTo(null);
        background1.setResizable(false);
        background1.setDefaultCloseOperation(3);
        
    }
    public String checkNumber()
    {
        Random a=new Random();
        Random b=new Random();
        Random c=new Random();
        Random d=new Random();
        int a1,a2,a3,a4;
        a1=a.nextInt(10);
        a2=a.nextInt(10);
        a3=a.nextInt(10);
        a4=a.nextInt(10);
        num=a1+""+a2+a3+a4;
        return num;
    }
    public void checkButton()
    {
        num=checkNumber();
        checkbutton=new Button(num);
        checkbutton.setBounds(240, 100, 80, 25);
        checkbutton.setBackground(Color.gray);
        checkbutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()== MouseEvent.BUTTON1)
                {
                    checkbutton.setVisible(false);
                    checkButton();
                    background1.add(checkbutton);
                }
            }
        });
    }
    public void showTime2()
    {
        JFrame background2=new JFrame();
        background2.setTitle("注册界面");
        background2.setLayout(null);//居中
        
        //账号设置
        user0 = new JLabel("账号");
        word3 = new JTextField("",15);
        user0.setBounds(100, 13, 60, 40);
        word3.setBounds(140, 20, 120, 25);
        word3.addActionListener(this);
        background2.add(user0);
        background2.add(word3);
        //密码设置
        password0 = new JLabel("密码");
        word5 =new JPasswordField("",15);
        password0.setBounds(100, 53, 60, 40);
        word5.setBounds(140, 60, 120, 25);
        background2.add(password0);
        background2.add(word5);
        //确定按钮
        yes = new Button("确定");
        yes.setBackground(Color.orange);
        yes.setBounds(75, 125, 240, 40);
        yes.addActionListener(this);
        yes.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                if (e.getButton() == MouseEvent.BUTTON1)
                    {
                    if(!word3.getText().equals("")&&!word5.getText().equals("")) 
                    {
                        flag=1;
                        for(int j=0;j<sum;j++)
                        {
                            if(zhanghao[j].contentEquals(word3.getText()))flag=0;
                        }
                        
                        if(flag==1)
                        {
                            zhanghao[sum]=word3.getText();
                            mima[sum]=word5.getText();
                            sum++;
                            JOptionPane.showMessageDialog(null, "注册成功!");
                            background2.setVisible(false);
                        }
                        else
                        {
                            word3.setText("");
                            word5.setText("");
                            JOptionPane.showMessageDialog(null, "该账号已存在!");
                        }    
                    }
                    else 
                    {
                        word3.setText("");
                        word5.setText("");
                        JOptionPane.showMessageDialog(null, "请输入完整信息!");
                    }
                    }
            }
            
        });
        background2.add(yes);
        
        background2.setVisible(true);
        background2.setSize(400, 250);
        background2.setLocationRelativeTo(null);
        background2.setResizable(false);
        
        
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand()=="登录")
        {
            flag1=0;
            flag2=1;
            flag3=1;
            num=checkNumber();
            for(int i=0;i<sum;i++)
            {
                if(word1.getText().contentEquals(zhanghao[i]))
                {
                    flag1=1;
                }
                if(word1.getText().contentEquals(zhanghao[i])&&!word4.getText().contentEquals(mima[i]))
                {
                    flag2=0;
                }
                if(word1.getText().contentEquals(zhanghao[i])&&word4.getText().contentEquals(mima[i])&&num.equals(word2.getText()))
                {
                    flag3=0;
                }
            }
            if(word1.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入用户名!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
                
            }
            else if(word4.getText().contentEquals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入密码!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
            }
            else if(word2.getText().contentEquals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入验证码!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
            }
            else if(flag1==0)
            {
                JOptionPane.showMessageDialog(null,"用户不存在!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
            }
            else if(flag2==0)
            {
                JOptionPane.showMessageDialog(null,"密码输入错误!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
            }
            else if(flag3==0)
            {
                JOptionPane.showMessageDialog(null,"验证码输入错误!");
                word1.setText("");
                word4.setText("");
                word2.setText("");
                checkbutton.setVisible(false);
                checkButton();
                background1.add(checkbutton);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "登陆成功!");
            }
        }
        else if(e.getActionCommand()=="注册")
        {
            showTime2();
        }
    }

}

运行截图如下:

posted @ 2019-10-17 17:50  yizhixiaozhu  阅读(1093)  评论(0编辑  收藏  举报