The following program demonstrates How to Create a Simple Login Application in Java.
Problem Statement
Create a login window to check user name and password using text fields and command buttons.
Solution
Creating a simple login window to check a username and password using text fields and command buttons in Java AWT can be done as follows.
import java.awt.*;
import java.awt.event.*;
public class LoginWindow extends Frame {
private TextField usernameField, passwordField;
private Button loginButton;
public LoginWindow() {
setTitle("Login Window");
setSize(300, 150);
setLayout(new FlowLayout());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Label usernameLabel = new Label("Username:");
usernameField = new TextField(20);
Label passwordLabel = new Label("Password:");
passwordField = new TextField(20);
passwordField.setEchoChar('*'); // To hide the entered password
loginButton = new Button("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
// Check username and password (you can replace this with your authentication logic)
if (isValidUser(username, password)) {
showMessage("Login Successful!");
} else {
showMessage("Invalid username or password. Please try again.");
}
}
});
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);
setVisible(true);
}
private boolean isValidUser(String username, String password) {
// Replace this with your actual authentication logic
// For simplicity, we're checking if the username is "user" and the password is "password"
return "user".equals(username) && "password".equals(password);
}
private void showMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
public static void main(String[] args) {
new LoginWindow();
}
}
In this program:
- We create a
LoginWindow
class that extendsFrame
to create the main login window. - Inside the constructor:
- We set the title, size, layout manager, and close operation for the frame.
- We add a
WindowListener
to handle window-closing events.
- Then, we create
Label
components for “Username” and “Password” andTextField
components for user input. We also set the password field to hide the entered characters. - Further, we create a
Button
for “Login” and add anActionListener
to it. When the button is clicked, it retrieves the entered username and password, checks them against theisValidUser
method (you should replace this with your actual authentication logic), and displays a message usingJOptionPane
. - Finally, we make the frame visible.
Compile and run this program, and you will see a simple login window where users can enter a username and password. When the “Login” button is clicked, it checks the credentials and shows a message accordingly.
Further Reading
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go