Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

How to make log-in program in java using GUI

Expert Solution
Check Mark
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.. ");

}

}

}

bartleby

Step by stepSolved in 3 steps with 1 images

[画像:Blurred answer]
Knowledge Booster
Background pattern image
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education