How to make log-in program in java using GUI

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter1: Creating Java Programs
Section: Chapter Questions
Problem 10RQ
icon
Related questions
Question

How to make log-in program in java using GUI

Expert Solution
Step 1

The below-given java program will obey the following rubrics:

 

  • Importing required packages.
  • Creating a login class, by extending JFrame class and implementing with ActionListener.
  • Inside the login class declaring some required fields.
  • declaring a constructor of the class, and inside this constructor defining two labels one for username and another for password.
  • Creating a submit button, adding the labels and buttons.
  • Setting title, size, etc. for swing output window.
  • In the main method calling the constructor.
  • Inside the event handler, checking if the entered username is “user” and password is “pass”, then displaying hello user, otherwise displaying “Invalid user”.
Step 2

Program code:

 

 

//importing packages

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

//creating login class and extending Jframe class with implemanting ActionListener

public class Login extends JFrame implements ActionListener

{

    //declaring required fields

    JPanel panel;

    JLabel user_label, password_label, message;

    JTextField userName_text;

    JPasswordField password_text;

    JButton submit, cancel;

   

    Login()

    {

       

        // User Label

        user_label = new JLabel();

        //displaying message to user

        user_label.setText("User Name :");

        userName_text = new JTextField();

       

        // Password

 

        password_label = new JLabel();

        password_label.setText("Password :");

        password_text = new JPasswordField();

 

        // Submit

 

        submit = new JButton("SUBMIT");

 

        panel = new JPanel(new GridLayout(3, 1));

 

        panel.add(user_label);

        panel.add(userName_text);

        panel.add(password_label);

        panel.add(password_text);

 

        message = new JLabel();

        panel.add(message);

        panel.add(submit);

       

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       

        // Adding the listeners to components..

        submit.addActionListener(this);

        add(panel, BorderLayout.CENTER);

        setTitle("Please Login Here !");

        setSize(500, 200);

        setVisible(true);

    }

     //main method

    public static void main(String[] args)

    {

        new Login();

    }

 

    @Override

    public void actionPerformed(ActionEvent ae) {

            //reading user name from user

        String userName = userName_text.getText();

        //reading pass from user

        String password = password_text.getText();

        //if username is user, and password is pass

        if (userName.trim().equals("user") && password.trim().equals("pass"))

        {

            //displaying hello to user

            message.setText(" Hello " + userName

                    + "");

        }

        //otherwise

        else {

            //displaying message Invalid user

            message.setText(" Invalid user.. ");

        }

 

    }

 

}

 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Multithreading Methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT