Java Login Interface

Source: Internet
Author: User

Design ideas:

Reads the user input information, including the account password, randomly generates a 6-bit string.
Verify that the verification code entered by the user is the same as the resulting random string.
If it's not the same, prompt and return to the re-entry interface.
If the same words will prompt the login success, and refresh the verification code to facilitate the next user's use.

Flow chart:

Original Program code:

Sun-C Hisense 1605-2
Package landing interface;
Import java.awt.Dimension;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;

Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPasswordField;
Import Javax.swing.JTextField;

Import Com.sun.javafx.tk.Toolkit;

Login Form Class
public class Login extends JFrame implements ActionListener, Runnable {
Private static final long serialversionuid = 1L;
Private String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Private StringBuffer SB;
Protected JLabel lblpersonal, Lblusername, lblpassword,str;
protected JTextField txtUserName;
protected JPasswordField Txtpassword;
protected JTextField txtpersonal;
Protected JButton btnlogin, Btnregister;
protected static thread thread = NULL;

public static void Main (string[] args) {
Data.init ();
Login Frmlogin = new Login ();
thread = new Thread (frmlogin);
Thread.Start ();
}

Public Login () {
Super ("Welcome to use the Software");

InitComponent ();
}

Initializing the control
public void InitComponent ()
{
Lblusername = new JLabel ("User name:");
Lblpassword = new JLabel ("Password:");
lblpersonal = new JLabel ("Verification Code:");

txtUserName = new JTextField (10);
Txtpassword = new JPasswordField (10);
txtpersonal = new JPasswordField (10);
Txtpersonal.setbounds (209, 400, 200, 400);
Btnlogin = new JButton ("login");
Btnregister = new JButton ("registration");
Txtpersonal.settext ("");


Btnlogin.addactionlistener (this);
Btnregister.addactionlistener (this);
Txtpersonal.addactionlistener (this);

This.setlayout (New GridLayout (4, 3));

This.add (Lblusername);
This.add (txtUserName);
This.add (Lblpassword);
This.add (Txtpassword);
This.add (lblpersonal);
Random random = new random ();
SB = new StringBuffer ();
for (int i = 0; i < 6; i++)
{
int number = Random.nextint (Base.length ());
Sb.append (Base.charat (number));
}
Str=new JLabel ("Verification Code:" +sb.tostring ());
Str.setsize (10,45);
Add (str);
This.add (txtpersonal);
This.add (Btnlogin);
This.add (Btnregister);

Txtusername.setfocusable (TRUE);
SetBounds (400,300,400,200);
This.setvisible (TRUE);
}


@Override
public void actionperformed (ActionEvent e) {
JButton btn = (JButton) e.getsource ();

if (btn = = Btnlogin) {
if (Txtusername.gettext (). Equals ("") | | Txtusername.gettext (). Trim (). Equals ("")) {
Joptionpane.showmessagedialog (This, "User name cannot be empty!") "," Login Failed ", joptionpane.error_message);
Return
}
if (Txtpassword.gettext (). Equals ("")) {
Joptionpane.showmessagedialog (This, "Login password cannot be empty!") "," Login Failed ", joptionpane.error_message);
Return
}
String userName = null;
String password = null;

UserName = Txtusername.gettext (). Trim ();
Password = Txtpassword.gettext ();
int i;

for (i=0; i < Data.customers.size (); i++) {
if (Data.customers.get (i). GetUserName (). Equals (UserName) && Data.customers.get (i). GetPassword (). Equals ( Password)) {
Break
}
}

if (I < Data.customers.size ()) {
Joptionpane.showmessagedialog (This, "welcome you," + UserName, "login Successful", joptionpane.information_message);
}
else {
Joptionpane.showmessagedialog (This, "Login failed, please check that the user name and password are correct ...", "Login Failed", joptionpane.error_message);
}
}
else if (btn = = Btnregister) {
This.dispose ();
New Register ();
}
}

@Override
public void Run () {
TODO auto-generated Method Stub

}
}


User Registration Form Class
Class Register extends JFrame implements ActionListener {
Private static final long serialversionuid = 1L;
Protected JLabel lblusername, Lblpassword, Lblconfirmedpassword;
protected JTextField txtUserName;
Protected JPasswordField txtpassword, Txtconfirmedpassword;
Protected JButton btnregister, btnreset;

Public Register () {
Super ("User registration");

InitComponent ();
}

Initializing the control
public void InitComponent () {
Lblusername = new JLabel ("User name:");
Lblpassword = new JLabel ("Password:");
Lblconfirmedpassword = new JLabel ("Confirm password:");

txtUserName = new JTextField (10);
Txtpassword = new JPasswordField (10);
Txtconfirmedpassword = new JPasswordField (10);

Btnreset = new JButton ("reset");
Btnregister = new JButton ("registration");

Btnreset.addactionlistener (this);
Btnregister.addactionlistener (this);

This.setlayout (New GridLayout (4, 2));
This.add (Lblusername);
This.add (txtUserName);
This.add (Lblpassword);
This.add (Txtpassword);
This.add (Lblconfirmedpassword);
This.add (Txtconfirmedpassword);
This.add (Btnregister);
This.add (btnreset);

Txtusername.setfocusable (TRUE);

SetBounds (400,300,600,400);
JFrame JFrame = new JFrame ("Landing Interface");
Jframe.setlocationrelativeto (NULL);
This.setvisible (TRUE);
This.pack ();
}

@Override
public void actionperformed (ActionEvent e) {
JButton btn = (JButton) e.getsource ();

if (btn = = Btnregister) {
if (Txtusername.gettext (). Equals ("") | | Txtusername.gettext (). Trim (). Equals ("")) {
Joptionpane.showmessagedialog (This, "User name cannot be empty!") "," Registration Failed ", joptionpane.error_message);
Return
}
if (Txtpassword.gettext (). Equals ("")) {
Joptionpane.showmessagedialog (This, "Login password cannot be empty!") "," Registration Failed ", joptionpane.error_message);
Return
}
if (Txtconfirmedpassword.gettext (). Equals ("")) {
Joptionpane.showmessagedialog (This, "the confirmation password cannot be empty!") "," Registration Failed ", joptionpane.error_message);
Return
}
if (! Txtconfirmedpassword.gettext (). Equals (Txtpassword.gettext ())) {
Joptionpane.showmessagedialog (This, "two times passwords must be the same!") "," Registration Failed ", joptionpane.error_message);
Return
}

String userName = null;
String password = null;
int i;

UserName = Txtusername.gettext (). Trim ();
Password = Txtpassword.gettext ();

for (i=0; i < Data.customers.size (); i++) {
if (Data.customers.get (i). GetUserName (). Equals (UserName)) {
Break
}
}

if (I < Data.customers.size ()) {
Joptionpane.showmessagedialog (This, "the user name has been registered, please choose another user name!") "," Registration Failed ", joptionpane.error_message);
}
else {
Data.customers.add (New Customer (userName, password));
Joptionpane.showmessagedialog (This, "congratulations" + UserName + "Registration is successful, please remember your username and password.") \ n click \ "OK \" button to start Login "," Registration succeeded ", Joptionpane.information_message);

This.dispose ();
New Login ();
}
}
else if (btn = = btnreset) {
Txtusername.settext ("");
Txtpassword.settext ("");
Txtconfirmedpassword.settext ("");
Txtusername.setfocusable (TRUE);
}
}
}

User Information class
Class Customer {
protected String userName = null;
protected String password = null;

Public Customer () {
}

Public Customer (String userName, string password) {
This.username = UserName;
This.password = password;
}

Public String GetUserName () {
return this.username;
}

public void Setusername (String userName) {
This.username = UserName;
}

Public String GetPassword () {
return This.password;
}

public void SetPassword (String password) {
This.password = password;
}
}

Collection class for caching user information
Class Data {
public static list<customer> customers = new arraylist<customer> ();

public static void Init () {
Customers.add (New Customer ("Fxk", "123456"));
}
}

Operation Result:

Java Login Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.