Login form creation in java Swing

    Let us create a login form using java swing. This is one of the interactive form designs in swing.

As usual login form needs a username and password and login option.

Program implementation:

Let us create the design.

  • ·       First, create a frame for login form.
  • ·       Two labels are created for username and password.
  • ·       A test field is generated for username. A password field is created for password.
  • ·       A button is designed for login purpose.

Steps to follow:

  • Import the built in package javax.swing.
  • Create a public class with main() function.
  • Let us create a objects for the following classes.
  • JFrame, JLabel, JTextField, JPasswordField , JButton.
  • Set the layout as null.
  • Set the bounds for all objects created.
  • Add the labels,textfield,passwordfield and button to frame.
  • setSize() -make the frame size.
  • setDefaultcloseOpertion() -make the default close operation.
  • setVisible() – sets as true for frame.

Program:

import javax.swing.*;

public class LoginFormEg {

    public static void main(String[] args) {

        JFrame f1 = new JFrame("Login Form");

        JLabel uLabel = new JLabel("Username:");

        JLabel pLabel = new JLabel("Password:");

        JTextField uField = new JTextField(15);

        JPasswordField pField = new JPasswordField(15);

        JButton loginButton1 = new JButton("Login");

        f1.setLayout(null);

         uLabel.setBounds(30, 30, 80, 25);

        uField.setBounds(120, 30, 150, 25);

        pLabel.setBounds(30, 70, 80, 25);

        pField.setBounds(120, 70, 150, 25);

        loginButton1.setBounds(120, 110, 80, 30);

        f1.add(uLabel);

        f1.add(uField);

        f1.add(pLabel);

        f1.add(pField);

        f1.add(loginButton1);

        f1.setSize(350, 200);

        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f1.setVisible(true);

    }

}

Output:

Just compile and run the program to get the output.

That’s all. The java swing program to implement the login form is implemented. Hope this program is useful to you. Keep coding!!!

No comments:

Post a Comment