banner image

Java Code Simple Login Form

login form java
It is easy to create Simple Login Form using java. You need basic knowledge about Java Programming to create a simple/basic login form. Obviously, login form has only two field one is username and another is password field. Here I have given a bunch of code of simple login form with explanation of each line.

In the begining, you see package kistcollege ie build automatically when you create new project but you must give name of project kistcollege.

Now after first line, you can see number of imported files.

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

All these above imported files directs the its special classes. When we declared objects like JFrame, JPanel, JLabel etc, all these must be imported which are contained in the Java.Swing package.

SampleGui is a class in which we create a simple login form.

In the SampleGui class we declare object variable which objects are later declared.

 JFrame frame;
   JPanel userPan, passwordPan, btnPan;
    JLabel userLab, passwordLab;
    JTextField userText, passwordText;
    JButton submitBtn;

After creating Object variables, there is created a public constructor which has no argument.
And within that constructor whole login form code is written. Before writing login form code, we check for errors using try catch error handling method.
public SampleGui()
    {
 try {
            UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel() );
        } catch (Exception e) {
            System.out.println("Here we get exception" + e);
        }
Now we go for creating object of every variable that we have created earlier.

 frame = new JFrame ("Login Form");
     
        userPan = new JPanel();
        passwordPan = new JPanel();
        btnPan = new JPanel();
     
        userLab = new JLabel("Username");
        passwordLab = new JLabel("Password");
     
        userText = new JTextField(20);
        passwordText = new JTextField(20);
     
        submitBtn = new JButton("Login");
     
 In this login form, we need a frame, so it is called frame object. Likewise, we need a user panel, password panel and button panel. There are also need a user label and password label along with user text field and password text field. Now one thing left and that is submit button.

Now we arrange them in their own label. As if, we put user label and user text in user panel, password label and password text in password panel. And we put submit button in button panel.

After that, we pack them in a frame

 frame.add(userPan, BorderLayout.NORTH);
        frame.add(passwordPan, BorderLayout.CENTER);
        frame.add(btnPan, BorderLayout.SOUTH);
     
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);

Now we set it visible true. That's it.
Java Code Simple Login Form Java Code Simple Login Form Reviewed by Ariyal on November 03, 2015 Rating: 5

No comments:

Post AD

Powered by Blogger.