Heim > Java > javaLernprogramm > Hauptteil

So implementieren Sie das Anmeldefenster in Java

王林
Freigeben: 2023-04-19 23:31:16
nach vorne
821 Leute haben es durchsucht

Hauptklasse des Anmeldefensters enthaltene Informationen

(Muster, Fragen)

package ccnu.paint;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ccnu.util.Answer;
import ccnu.util.Verification;

public class Login extends JFrame
{
    private static final long serialVersionUID = 1L;

    private Properties pro = new Properties();

    private boolean ver_code = false; // 默认输入验证码错误

    private Answer answer = null;

    private JPanel p1 = new JPanel(); // 添加到JPanel中的组件默认为流式布局
    private JLabel luser = new JLabel("username: ");
    private JTextField username = new JTextField(20);

    private JPanel p2 = new JPanel();
    private JLabel lpwd = new JLabel("password: ");
    private JPasswordField pwd = new JPasswordField(20);

    private JPanel p4 = new JPanel();
    private JLabel lVer = new JLabel("verification: ");
    private JTextField ver = new JTextField(10);
    private JLabel img = new JLabel();
    private JLabel result = new JLabel();

    private JPanel p3 = new JPanel();
    private JButton ok = new JButton("ok");
    private JButton cancel = new JButton("cancel");
    private JButton signUp = new JButton("Sign up"); // 用于账户注册

    // 设置组件的监听
    public void initListener()
    {
        username.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {// JTextField的action是回车键
                String name = username.getText();
                // Login.this.setTitle(name);
                // System.out.println(name.hashCode() + "***" +"".hashCode());
                if (name.equals(""))
                {
                    JOptionPane.showMessageDialog(Login.this, "Please input a userName!");
                } else
                {
                    pwd.grabFocus();
                }
            }
        });

        pwd.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String password = new String(pwd.getPassword());
                if(password.equalsIgnoreCase(""))
                {
                    JOptionPane.showMessageDialog(Login.this, "please input a password!");
                }else{
                    ver.grabFocus();
                }
            }

        });

        ok.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e)
            {
                // 重新加载最新的账户文件
                try
                {
                    pro.load(new FileInputStream(new File("src/res/accouts.properties")));
                } catch (IOException e1)
                {
                    e1.printStackTrace();
                }
                check();
            }

        });

        // 判断验证码是否正确
        ver.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String verCode = ver.getText();
                if(verCode.equals(""))
                {
                    JOptionPane.showMessageDialog(Login.this, "Please input a verification!");
                }else{
                    if(verCode.equals(answer.getResult()))
                    {
                        result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg"))); // 显示提示的图片信息(如√图片)
                        ver_code = true;
                        // 检查之前,重新加载最新的账户文件
                        try
                        {
                            pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 将账户文件加载进来
                        } catch (IOException e1)
                        {
                            e1.printStackTrace();
                        }
                        check();
                    }else{
                        result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg"))); // 显示提示的图片信息(如×图片) 
                        ver_code = false;
                    }
                }
            }
        });

        // 点击图片会更改验证码
        img.addMouseListener(new MouseAdapter(){

            @Override
            public void mouseClicked(MouseEvent e)
            {
                answer = Verification.verification();
                img.setIcon(new ImageIcon(answer.getBufferedImage())); // 设置验证码图案
            }
        });

        cancel.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int option = JOptionPane.showConfirmDialog(Login.this, "Are you sure to exit?");
//              System.out.println("option = " + option);
                if (option == 0)
                {// Yes
                    Login.this.dispose();
                }
            }
        });

        signUp.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e)
            {
                new SignUp();
            }
        });
    }

    // 初始化登录窗口及其组件的设置监听
    public Login()
    {
        super("Login");

        // 加载账户文件
        try
        {
            pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 从指定位置将账户文件加载进来
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        initListener();
        answer = Verification.verification(); // 生成验证码
        img.setIcon(new ImageIcon(answer.getBufferedImage())); // 设置初始验证码

        this.setLocation(new Point(200, 200));
        this.setSize(500, 300);
        this.setLayout(new GridLayout(4, 1, 0, 20)); // 垂直间隙为20px
        p1.add(luser);
        p1.add(username);
        p2.add(lpwd);
        p2.add(pwd);
        p4.add(this.lVer);
        p4.add(this.ver);
        p4.add(this.img);
        result.setForeground(Color.red);
        result.setFont(new Font("楷体", Font.BOLD, 20));
        p4.add(result);
        p3.add(ok);
        p3.add(cancel);
        p3.add(signUp);
        this.add(p1);
        this.add(p2);
            this.add(p4);
        this.add(p3);

//      this.setBackground(Color.blue); // JFrame的上层还有一个ContentPane
        this.getContentPane().setBackground(Color.gray);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 等价于Frame中的windowClosing事件
    }

    // 检查用户名或密码
    public void check()
    {

        String verCode = ver.getText();
        if(verCode.equals(""))
        {
            JOptionPane.showMessageDialog(Login.this, "Please input a verification!");
            return;
        }else{
            if(verCode.equals(answer.getResult()))
            {
                result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg")));
                ver_code = true;
            }else{
                result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg")));
                ver_code = false;
            }
        }

        if(ver_code == false)
        {
            JOptionPane.showMessageDialog(this, "verification is error!");
            return;
        }
        String name = username.getText();
        String password = new String(pwd.getPassword()); // return char[]
//      if (name.equalsIgnoreCase("admin") && password.equals("123456"))
        if (isPass(name, password))
        {
//          new PaintApp(name);
            JOptionPane.showMessageDialog(this, "-^_^-   OK..."); // 此处可以加上其他的登陆成功后进一步处理的窗口
            this.dispose();
        } else
        {
            JOptionPane.showMessageDialog(this, "userName or password is incorrect!");
            username.setText("");
            pwd.setText("");
            ver.setText("");
            answer = Verification.verification();
            img.setIcon(new ImageIcon(answer.getBufferedImage()));
            result.setIcon(null);
        }
    }

    // 验证用户输入的账户名和密码是否正确(通过与加载进来的账户 pro 比对)
    public boolean isPass(String name, String password)
    {
        Enumeration en = pro.propertyNames();
        while(en.hasMoreElements())
        {
            String curName = (String)en.nextElement();
//          System.out.println(curName + "---" + pro.getProperty(curName));
            if(curName.equalsIgnoreCase(name))
            {
                if(password.equalsIgnoreCase(pro.getProperty(curName)))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args)
    {
        new Login();
    }
}
Nach dem Login kopieren
Die Datei Words.txt für das Problem der Erkennung chinesischer Schriftzeichen bei der Generierung des Verifizierungscodes.

Zum Beispiel: die Datei Words2.txt für das Problem der Generierung der arithmetischen Operation des Verifizierungscodes von der Schule of Political Science and Law of Handong University, Wuhan City, Hubei Province, China

123456789+-

Promptbild

Anmeldeeffekt

Das obige ist der detaillierte Inhalt vonSo implementieren Sie das Anmeldefenster in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:yisu.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage