How to implement login window in Java
要求:用户名和密码正确后进入首页,错误提示错误,并且3次错误后将退出窗口。
1、主窗口,窗口上有文本输入框和登陆按钮,如图
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
