LoginForm source code:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
javax.swing.event.*;
import
java.io.*;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
class LoginForm extends JFrame implements
ActionListener{
JPanel panel;
JLabel lblname;
JLabel lblpassword;
JLabel lblmess;
JTextField txtname;
JPasswordField txtpassword;
JButton btlogin;
JPanel panelreg;
JLabel lblnamereg;
JLabel lblpasswordreg;
JLabel lblmessreg;
JTextField txtnamereg;
JPasswordField txtpasswordreg;
JButton btsubmit;
LoginForm(){
1 setTitle("Member
login and Registration");
2 setSize(630,250);
3 setResizable(false);
4 setDefaultCloseOperation(EXIT_ON_CLOSE);
5 Container
content=getContentPane();
6 JDesktopPane des=new
JDesktopPane();
//Login form
7 JInternalFrame flog=new
JInternalFrame();
8 flog.setSize(300,200);
9 flog.setLocation(10,2);
10 flog.setTitle("Member
Login");
11 lblname=new
JLabel("User Name:");
12 lblpassword=new
JLabel("Password:");
13 lblmess=new
JLabel("");
14 btlogin=new
JButton("Login");
15 btlogin.addActionListener(this);
17 txtname=new JTextField(20);
18 txtpassword=new
JPasswordField(20);
19 panel=new JPanel();
20 panel.add(lblname);
21 panel.add(txtname);
22 panel.add(lblpassword);
23 panel.add(txtpassword);
24 panel.add(btlogin);
25 panel.add(lblmess);
26 flog.add(panel);
27 flog.setVisible(true);
//Registration form
28 JInternalFrame freg=new
JInternalFrame();
29 freg.setSize(300,200);
30 freg.setLocation(315,2);
31 freg.setTitle("Member
Registration");
32 lblnamereg=new
JLabel("User Name:");
33 lblpasswordreg=new
JLabel("Password:");
34 lblmessreg=new
JLabel("");
35 btsubmit=new
JButton("Submit");
36 btsubmit.addActionListener(this);
37 btlogin.addActionListener(this);
38 txtnamereg=new
JTextField(20);
39 txtpasswordreg=new
JPasswordField(20);
40 txtpasswordreg.addKeyListener(new
KeyList());
41 panelreg=new JPanel();
42 panelreg.add(lblnamereg);
43 panelreg.add(txtnamereg);
44 panelreg.add(lblpasswordreg);
45 panelreg.add(txtpasswordreg);
46 panelreg.add(btsubmit);
47 panelreg.add(lblmessreg);
48 freg.add(panelreg);
49 freg.setVisible(true);
50 des.add(flog);
51 des.add(freg);
52 content.add(des,
BorderLayout.CENTER);
53 setVisible(true);
54 txtname.requestFocus();
}
55 public
void actionPerformed(ActionEvent e){
if(e.getSource()==btsubmit){
String
uname=txtnamereg.getText();
String passw=new
String(txtpasswordreg.getPassword());
if(!checkBlank(uname,passw,
lblnamereg,lblpasswordreg)){
if(!checkExist("accounts.txt",uname)){
passw=new
String(encrypt(passw));
String
accinfo=uname+"-"+passw;
saveToFile("accounts.txt",accinfo);
}
}
}
else if(e.getSource()==btlogin){
String
uname=txtname.getText();
String passw=new
String(txtpassword.getPassword());
if(!checkBlank(uname,passw,lblname,lblpassword))
validateUser("accounts.txt",uname,passw);
}
}
56 public
class KeyList extends KeyAdapter{
public
void keyPressed(KeyEvent ke){
String passw=new
String(txtpasswordreg.getPassword());
String
mess=checkStrength(passw);
showMess(mess+"
password",lblpasswordreg);
}
}
57 public
boolean checkBlank(String name, String passw, JLabel namemess, JLabel
passwmess){
boolean hasBlank=false;
if(name.length()<1){
showMess("User name
is required.",namemess);
hasBlank=true;
}
if(passw.length()<1){
showMess("Password
is required.",passwmess);
hasBlank=true;
}
return hasBlank;
}
58 public
void showMess(String mess, JLabel lbl){
lbl.setText(mess);
lbl.setForeground(Color.RED);
}
59 public
String checkStrength(String passw){
Pattern pat=Pattern.compile("([0-9][aA-zZ]|[aA-zZ][0-9])");
Matcher mat=pat.matcher(passw);
if(mat.find()) {
if(passw.length()>=8)
return "Strong";
else return
"Medium";
}
else return "Weak";
}
60 public
void reset(JLabel lblname,JLabel lblpassw ){
lblname.setText("User
Name:");
lblname.setForeground(Color.BLACK);
lblpassw.setText("Password:");
lblpassw.setForeground(Color.BLACK);
}
61 public
void validateUser(String filename, String name, String password){
FileReader fr;
BufferedReader br;
boolean valid=false;
String accinfo;
try{
fr=new
FileReader(filename);
br=new
BufferedReader(fr);
while
((accinfo=br.readLine())!=null){
if(check(accinfo,name,password))
{
showMess("Valid
login",lblmess);
valid=true;
break;
}
}
if(!valid) showMess("Invalid
login",lblmess);
reset(lblname,lblpassword);
br.close();
fr.close();
} catch(Exception
ie){System.out.println("Error!");}
}
62 public
boolean check(String accinfo, String name, String passw){
String[] info=accinfo.split("-");
String uname=info[0];
String pass=new
String(decrypt(info[1]));
if(uname.equals(name)
&& pass.equals(passw))
return true;
else return false;
}
63 public
boolean checkExist(String filename, String name){
FileReader fr;
BufferedReader br;
String accinfo;
boolean exist=false;
try{
fr=new
FileReader(filename);
br=new
BufferedReader(fr);
while
((accinfo=br.readLine())!=null){
if(check(accinfo,name))
{
showMess("The
account already exists.",lblmessreg);
exist=true;
break;
}
}
br.close();
fr.close();
} catch(Exception ie){System.out.println("Error!");}
return exist;
}
64 public
boolean check(String accinfo, String name){
String[]
info=accinfo.split("-");
String uname=info[0];
if(uname.equals(name))
return true;
else return false;
}
65 public
void saveToFile(String filename,String text){
try{
FileWriter
fw=new FileWriter(filename,true);
BufferedWriter
bw=new BufferedWriter(fw);
bw.write(text);
bw.newLine();
bw.flush();
bw.close();
showMess("The account is
created.",lblmessreg);
reset(lblnamereg,lblpasswordreg);
}catch(IOException
ie){System.out.println("Error in writing to file...");}
}
66 public
byte[] encrypt(String passw){
byte[]
sb=passw.getBytes();
int i;
for(i=0;i<sb.length;i++)
sb[i]=(byte)(sb[i]+1);
return(sb);
}
67 public
byte[] decrypt(String passw){
byte[]
sb=passw.getBytes();
int i;
for(i=0;i<sb.length;i++)
sb[i]=(byte)(sb[i]-1);
return(sb);
}
}
68 public
class LoginProgram{
public static
void main(String args[]){
new LoginForm();
}
}
Code Explanation:
1 Set the title of the program window (Member Login and Registration).
2 Specify the width and height of the program window when it opens.
3 Disable program window resizing.
4 Let the program window close when the user clicks the close button.
5 Create a container object to act as the controls placeholder.
6 Create a JDesktopPane des object . All JInternalFrame objects will be placed on this des object. In this program, we need two JInternalFrame objects, flog and freg. One JInternalFrame (flog) acts as a login form and another (freg) acts as registration form.
7-27 Create the login form interface. On the login form, there are two text boxes, three labels, and one button. The two text boxes are txtname and txtpassword. The txtname text box allows the user to enter the user name and txtpassword text box is used for password input. The three labels are lblname to display "User Name" text, lblpassword to display "Password", and lblmess to display "Valid login" or "Invalid login" text on the login form. The btlogin button is clicked to login to program.
28-49 Create the registration form interface. On the registration form, there are also two text boxes, three labels, and one button. The two text boxes are txtnamereg and txtpasswordreg. The txtnamereg text box is used to allow the user to enter the user name and txtpasswordreg text box is used for password input. The three labels are lblnamereg to display "User Name" text, lblpasswordreg to display "Password", and lblmessreg to display "The account is created." or "The account already exists." text on the registration form. The btsubmit button is clicked to save to account information to the text file.
50 Place the flog internal frame in the des object.
51 Place the freg internal frame in the des object.
52 Place the des object in the container.
53 Make sure the program window visible.
54 Set focus on the txtname text box of the login form.
55 The code in the actionPerformed method is written to enable the btlogin and btsubmit buttons to perform their tasks when the user clicks them. Once the btlogin is clicked, the account information input by the user in the txtname, and txtpasswod text boxes are searched through the accounts. txt file. If it is found, the "Valid login" text is displayed. Otherwise, the "Invalid login" is displayed. If the btsubmit is clicked, firstly it checks whether the user name and password are not blank. Then the user name and password are searched through the accounts file to check account existing. If the account does not exist, it is saved to the file.
56 The KeyList class extends the KeyAdapter class to rewrite the keyPressed method of the KeyAdpater class. This method will enable the txtpasswordreg text to check its strength when the key is pressed by invoking the checkStrength method.
57 The checkBlank method is implemented to determine whether the user does not enter the values in the
txtpassword, txtname, txtpasswordreg, or txtnamereg text box.
58 The showMess method is invoked to show the text "The account is created." or "The account already exists." on the registration form or to show the text "Valid login." or "Invalid login" on the login form.
59 The checkStrength method defines the code to check whether the input password is strong. A strong password should be the combination of numbers and alphabetical characters and more than eight characters long. To determine whether the password contains the combination of numbers and alphabetical characters, we use Java regular expression. The pattern to match this criteria is ([0-9][aA-zZ]|[aA-zZ][0-9]).
60 The reset method resets text on lblname and lblpassword or lblnemreg and lblpasswordreg to their original text.
61 The validateUser is implemented to check whether the user name and password input are in the accounts.txt file. If they exist, it a valid login.
62 The check(String accoinfo,String name, String passw) mehtod is invoked by the valideUser method to compare the user name and password input by the user with the account information read from the file.
63 The checkExist method checks whether the account info exists in the file. If it exists, saving is not allowed.
64 The check(String accoinfo,String name) mehtod is invoked by the checkExist method to compare the user name input by the user with the user name read from the file. In Java, you can have many methods with the same name. However, those methods must be different from each other by return type, number of arguments, or argument type.
65 The saveToFile method is invoked to save the account informaton to the accounts.txt file. One account (username-password) is written in a single line.
66 The encrypt method transforms a password to a different value before it is saved in the file . This is to ensure account security. The password is encrypted by changing all bytes of the orgical password value (add 1 to every byte).
67 The decrypt method transforms the encrypted password back to its orginal value before it is compared with the password input by the user to login. The password is decrypted by subtracting 1 from every byte of the encrypted password.
68 LoginProgram class has the main method to start the program.
Thanks!!!
ReplyDeletehi i need a login page for my site can u help me???
ReplyDeleteYou need to implement the login page for your website by using one of the following web programming languages: PHP, ASP.NET, JavaScript, or JSP. This is a login page example http://www.worldbestlearningcenter.com/index_files/asp_net-login-form.htm.
DeleteGud job ...
ReplyDeleteThanks!!!
is all this code saved as one class file?
ReplyDeleteYes. The name of the class is LoginForm.
Deletehi,I keep getting an error on the while loops in 61 and 63. It says incompatible types
ReplyDeleteYea I'm getting an error with the source code. Can anyone help me?
ReplyDeleteits too good but when i run it in my code window appear infinite times .....
ReplyDeleteI appreciate, cause I found exactly what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. website development
ReplyDelete