登陆界面

package Login;
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.*;
public class NewGUI extends JFrame implements ActionListener{
    private JLabel name,pass,yzm,code,jpg;
    private JTextField tf1,tf3,tf4;
    private JPasswordField tf2,tf5;
    private Button login,register1,register2,jp1;
    private JPanel back,jp2;
    private JFrame j1,j2;
    private Icon icon;
    static String str,user,pwd;
    
    
    public static void main(String[] args) {
    NewGUI ng = new NewGUI();
    ng.ShowFrame();
    }
    
    public void ShowFrame() {
        j1 = new JFrame();
        j1.setTitle("登录窗口");
        //j.setLayout(new FlowLayout());
        j1.setLayout(null); //自定义布局
        
        //设置账号及其输入框
        name = new JLabel("账号:");
        Font font = new Font("楷体", Font.PLAIN, 15);
        tf1 = new JTextField("用户名/邮箱",20);
        tf1.setFont(font);
        tf1.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent e)
                    {
                        if (e.getButton() == MouseEvent.BUTTON1)
                            tf1.setText("");
                    }
                });
        name.setBounds(70, 13, 60, 40);
        tf1.setBounds(110, 20, 210, 25);
        tf1.addActionListener(this);
        
        //设置密码及其输入框
        pass = new JLabel("密码:");
        tf2 = new JPasswordField(20);
        pass.setBounds(70, 53, 60, 40);
        tf2.setBounds(110, 60, 210, 25);
        
        //设置验证码输入框并添加到面板三
        yzm = new JLabel("验证码:");
        tf3 = new JTextField(5);
        yzm.setBounds(58, 93, 60, 40);
        tf3.setBounds(110, 100, 100, 25);
        
        //设置面板来存验证码
            paint();
        
        //设置登陆按钮
        login = new Button("登陆");
        login.setBackground(Color.orange);
        login.setBounds(80, 150, 240, 40);
        login.addActionListener(this);
        
        //设置注册按钮
        register1 = new Button("注册");
        register1.setBackground(Color.orange);
        register1.setBounds(80, 205, 240, 40);
        register1.addActionListener(this);
        
//        //设置背景面板
//        jp2 = new JPanel();
//        jp2.setSize(400, 300);
//        jp2.setBackground(Color.white);    
        
        //将组件放到窗口上
        j1.add(name);
        j1.add(tf1);
        j1.add(pass);
        j1.add(tf2);
        j1.add(yzm);
        j1.add(tf3);
        j1.add(login);
        j1.add(register1);
        j1.add(jpg);
        j1.add(jp1);
      //  j1.add(jp2);
        j1.setVisible(true);
        j1.setSize(400, 300);
        j1.setLocationRelativeTo(null);
        j1.setResizable(false);
        j1.setDefaultCloseOperation(3);
    }
    public void Register() {
        JFrame j2 = new JFrame();
        j2.setTitle("注册界面");
        //j.setLayout(new FlowLayout());
        j2.setLayout(null); //自定义布局
        
        //设置账号及其输入框
        name = new JLabel("账号:");
        Font font = new Font("楷体", Font.PLAIN, 15);
        tf4 = new JTextField("用户名/邮箱",20);
        tf4.setFont(font);
        tf4.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent e)
                    {
                        if (e.getButton() == MouseEvent.BUTTON1)
                            tf4.setText("");
                    }
                });
        name.setBounds(100, 13, 60, 40);
        tf4.setBounds(140, 20, 120, 25);
        tf4.addActionListener(this);
        
        //设置密码及其输入框
        pass = new JLabel("密码:");
        tf5 = new JPasswordField(20);
        pass.setBounds(100, 53, 60, 40);
        tf5.setBounds(140, 60, 120, 25);
        
        //设置注册按钮
        register2 = new Button("立即注册");
        register2.setBackground(Color.orange);
        register2.setBounds(75, 125, 240, 40);
        register2.addActionListener(this);
        register2.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                if (e.getButton() == MouseEvent.BUTTON1)
                    {
                    if(!tf4.getText().equals("")&&!tf5.getText().equals("")) {
                        user=tf4.getText();
                        pwd=tf5.getText();
                        JOptionPane.showMessageDialog(null, "注册成功!");
                        j2.setVisible(false);}
                        else {
                            tf4.setText("");
                            tf5.setText("");
                            JOptionPane.showMessageDialog(null, "请输入完整信息!");
                        }
                    }
            }
        });
        
//        //设置背景面板
//        jp2 = new JPanel();
//        jp2.setSize(400, 250);
//        jp2.setBackground(Color.white);    
        
        //将组件放到窗口上
        j2.add(name);
        j2.add(tf4);
        j2.add(pass);
        j2.add(tf5);
        j2.add(register2);
//        j2.add(jp2);
        j2.setVisible(true);
        j2.setSize(400, 250);
        j2.setLocationRelativeTo(null);
        j2.setResizable(false);
    }
    public  String BulidChar() {
        String str1="";
        for(int i=0;i<4;i++) {
        int x=0;
        int a = (int)(Math.random()*3);
        if(a==0) {
                x = (int)(Math.random()*10+48);
        }
        if(a==1) {
                x = (int)(Math.random()*26+65);
        }
        if(a==2) {
                x = (int)(Math.random()*26+97);
        }
            str1=str1+(char)x;
            }
        return str1;
    }
    public void paint() {
        icon = new ImageIcon("src/1.png");
        jpg = new JLabel();
        jpg.setIcon(icon);
        jpg.setBounds(215, 102, 20, 20);
        str=BulidChar();
        jp1 = new Button(str);
        jp1.setBounds(240, 100, 80, 25);
        jp1.setBackground(Color.gray);
        jp1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()== MouseEvent.BUTTON1)
                {
                    jp1.setVisible(false);
                    paint();
                    j1.add(jp1);
                }
            }
        });
    }
    public void actionPerformed(ActionEvent e) {
        
        if(e.getActionCommand()=="登陆") {
            if(tf1.getText().equals("用户名/邮箱")||tf1.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入用户名!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if(tf2.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入密码!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if(tf3.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null,"请输入验证码!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if((!str.equals(tf3.getText())))
            {
                JOptionPane.showMessageDialog(null, "验证码错误!"+str);
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if(!tf1.getText().equals(user))
            {
                JOptionPane.showMessageDialog(null,"用户名不存在!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
//            else if(((tf1.getText()).equals(user))&&(tf2.getText().equals("")))
//            {
//                JOptionPane.showMessageDialog(null,"请输入密码!");
//                tf1.setText("");
//                tf2.setText("");
//                tf3.setText("");
//                j1.setVisible(false);
//                ShowFrame();
//                paint();
//            }
            else if(((tf1.getText()).equals(user))&&(!(tf2.getText()).equals(pwd)))
            {
                JOptionPane.showMessageDialog(null,"密码不正确!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if(((tf1.getText()).equals(user))&&((tf2.getText()).equals(pwd))&&(tf3.getText().equals("")))
            {
                JOptionPane.showMessageDialog(null, "请输入验证码!");
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else if(((tf1.getText()).equals(user))&&(!(tf2.getText()).equals(pwd))&&(!str.equals(tf3.getText())))
            {
                JOptionPane.showMessageDialog(null, "验证码错误!"+str);
                tf1.setText("");
                tf2.setText("");
                tf3.setText("");
                jp1.setVisible(false);
                paint();
                j1.add(jp1);
            }
            else{
                JOptionPane.showMessageDialog(null, "登陆成功!");
            }
        }
        else if(e.getActionCommand()=="注册") {
            Register();
        }
    }
}

功能:

(1)未输入用户名便登陆

 

 

(2)未输入密码便登陆

 

 

(3)未输入验证码便登陆

 

 

(4)输入账号密码,验证码错误

 

 

(5)账号密码输入,验证码输入正确,但用户名不存在

 

 

(6)注册

 

 

(7)注册成功

 

 

(8)账号存在,验证码正确,密码错误

 

 

(9)账号密码验证码正确

 

 

(10)单击验证码,更换验证码

 

变为

 

posted @ 2019-09-18 21:05  Nevesettle  阅读(294)  评论(0编辑  收藏  举报