search
HomeJavajavaTutorialHow to implement login window in Java

要求:用户名和密码正确后进入首页,错误提示错误,并且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);
        
    }
}

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)