How to make log-in program in java using GUI
How to make log-in
![](/static/compass_v2/shared-icons/check-mark.png)
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”.
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.. ");
}
}
}
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)