JAVA Program using Swing to Design Login Form

JAVA Program using Swing to Design Login Form:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SwingFirstLogin {

    public static void main(String[] args) {
        // Creating instance of JFrame
        JFrame frame = new JFrame("My First Swing Example");
        // Setting the width and height of frame
        frame.setSize(350, 200);

        /* Creating panel. This is same as a div tag in HTML
         * We can create several panels and add them to specific
         * positions in a JFrame. Inside panels we can add text
         * fields, buttons and other components.
        JPanel panel = new JPanel();
        // adding panel to frame
        /* calling user defined method for adding components
         * to the panel.

        // Setting the frame visibility to true

    private static void placeComponents(JPanel panel) {

        /* We will discuss about layouts in the later sections
         * of this tutorial. For now we are setting the layout
         * to null


        // Creating JLabel

        JLabel userLabel = new JLabel("User");

        /* This method specifies the location and size
     * of component. setBounds(x, y, width, height)
     * here (x,y) are cordinates from the top left
     * corner and remaining two arguments are the width
     * and height of the component.


    /* Creating text field where user is supposed to
     * enter user name.

        JTextField userText = new JTextField(20);

        // Same process for password label and text field.

        JLabel passwordLabel = new JLabel("Password");

    /*This is similar to text field but it hides the user
         * entered data and displays dots instead to protect
     * the password like we normally see on login screens.

        JPasswordField passwordText = new JPasswordField(20);

         // Creating login button

        JButton loginButton = new JButton("login");
        loginButton.setBounds(10, 80, 80, 25);



JAVA Program using Swing to Design Login Form

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.