Home > Java > javaTutorial > body text

How to implement login window in Java

WBOY
Release: 2023-05-13 18:28:06
forward
1271 people have browsed it

要求:用户名和密码正确后进入首页,错误提示错误,并且3次错误后将退出窗口。

1、主窗口,窗口上有文本输入框和登陆按钮,如图

How to implement login window in Java

2、在登陆按钮和取消按钮上设置时间监听机制,

取消:退出程序;

登陆:获取用户名和密码,然后与自己保存的用户名密码作比较,现在两种情况,正确与错误,

正确:关闭登陆窗口,并弹出新窗口,新窗口即为首页;

错误:弹出新窗口,提示错误,并且累加,超过三次则推出程序,

现在粘上代码,

import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
 
public class Test10_3 {
    public static void main(String [] args){
        Denglu de = new Denglu();
    }
}
 
class Denglu{
    private JFrame jf = new JFrame("登陆窗口");
    private JButton jb1 = new JButton("用户名");
    private JButton jb2 = new JButton("密码");
    private JButton jbLogin = new JButton("登陆");
    private JButton jbquxiao = new JButton("取消");
    private JTextField jtName = new JTextField();
    private JTextField jtPwd = new JTextField();
    private final String mima = "sss111";//此为预设的密码
    private int count = 0;
    
    private JFrame jfZhu = new JFrame();
    ImageIcon image = new ImageIcon("C:\\Users\\sss\\Desktop\\index.jpg");//这为我的图片路径
    private JLabel jlZhu = new JLabel(image);
    
    private JFrame jfCuo = new JFrame();
    private JLabel jlCuo = new JLabel("你输入的用户名密码不匹配!");
 
    public Denglu(){
        Container cp = jf.getContentPane();
        cp.setLayout(null);
        
        jtName.setBounds(150,50,100,20);
        cp.add(jtName);
        jb1.setBounds(10,50,100,20);
        cp.add(jb1);
 
        jtPwd.setBounds(150,100,100,20);
        cp.add(jtPwd);
        jb2.setBounds(10,100,100,20);
        cp.add(jb2);
        
        jbLogin.setBounds(20, 150, 100, 20);
        jbquxiao.setBounds(150, 150, 100, 20);
        cp.add(jbLogin);
        cp.add(jbquxiao);
        
        jbquxiao.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        
        jbLogin.addActionListener(new ActionListener(){
            
            public void actionPerformed(ActionEvent e){
                String name = jtName.getText();
                String pwd = jtPwd.getText();
                String aa = name+pwd;
                if(aa.equals(mima)){            //Java中判断两个字符串相等用equals()函数。
                    jf.dispose();    //关闭登陆面板
                    Container cp2 = jfZhu.getContentPane();
                    cp2.setLayout(null);
                    
                    jlZhu.setBounds(0, 0, 400, 300);
                    cp2.add(jlZhu);
                    jfZhu.setSize(400,300);
                    jfZhu.setVisible(true);
                }
                else{
                    if(0<=count&&count<3){
                        Container cp3 = jfCuo.getContentPane();
                        cp3.setLayout(null);
                        
                        jlCuo.setBounds(0, 0, 400, 300);
                        cp3.add(jlCuo);
                        jfCuo.setSize(400,300);
                        jfCuo.setVisible(true);
                        count++;
                    }
                    else{
                        if(count>=3)
                            System.exit(0);
                    }
                }
            }
        });
        
        jf.setSize(400, 300);
        jf.setVisible(true);
        
    }
}
Copy after login

The above is the detailed content of How to implement login window in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template