Develop a swing application for login validation of a system as shown in the figure. The
application shows two options: one Sign up and another Sign in. When Sign up is selected, a
new screen will appear for asking name, username, password. If the entered username and
password do not exist in the database, it will show a successful message [Name] has
successfully registered and store corresponding username, password in the database,
otherwise the message [Name] already exists in the system will be displayed. On the other
hand, when Sign In option is selected, the user will be prompted to enter username,
password as shown in the figure. Eventually, the application checks whether username and
password both match with some entry in the database. If such entry found in the database,
then a welcome message Welcome [Name] will appear, otherwise an unsuccessful login
message Wrong username/password will be displayed. For message display, the swing dialogs can be used.
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
static HashMap<String, String> database = new HashMap<>();
public static void main(String[] args) {
database.put("admin", "admin");
JFrame signInFrame = new JFrame("SignIn");
JPanel signInPanel = new JPanel();
signInPanel.setLayout(new BoxLayout(signInPanel, BoxLayout.Y_AXIS));
JTextField userNameSITextField = new JTextField();
userNameSITextField.setToolTipText("Username");
userNameSITextField.setMaximumSize(new Dimension(120, 20));
userNameSITextField.setAlignmentX(Component.CENTER_ALIGNMENT);
userNameSITextField.setAlignmentY(Component.CENTER_ALIGNMENT);
JTextField passwordSITextField = new JTextField();
passwordSITextField.setMaximumSize(new Dimension(120, 20));
passwordSITextField.setToolTipText("Password");
passwordSITextField.setAlignmentX(Component.CENTER_ALIGNMENT);
passwordSITextField.setAlignmentY(Component.CENTER_ALIGNMENT);
JButton signInJButton = new JButton("Sign in");
signInJButton.setAlignmentX(Component.CENTER_ALIGNMENT);
signInJButton.setAlignmentY(Component.CENTER_ALIGNMENT);
signInJButton.addActionListener(event -> {
if (userNameSITextField.getText().length() > 0 && passwordSITextField.getText().length() > 0) {
if (database.get(userNameSITextField.getText()) != null
&& database.get(userNameSITextField.getText()).equals(passwordSITextField.getText())) {
JOptionPane.showMessageDialog(signInFrame, "Welcome " + userNameSITextField.getText(),
"Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(signInFrame, "Wrong username/password",
"Fail", JOptionPane.INFORMATION_MESSAGE);
}
}
});
signInPanel.add(Box.createGlue());
signInPanel.add(userNameSITextField);
signInPanel.add(Box.createRigidArea(new Dimension(0, 10)));
signInPanel.add(passwordSITextField);
signInPanel.add(Box.createRigidArea(new Dimension(0, 10)));
signInPanel.add(signInJButton);
signInPanel.add(Box.createGlue());
signInFrame.add(signInPanel);
signInFrame.setSize(320, 250);
signInFrame.setResizable(false);
signInFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JFrame signUpFrame = new JFrame("Sign Up");
JPanel signUpPanel = new JPanel();
signUpPanel.setLayout(new BoxLayout(signUpPanel, BoxLayout.Y_AXIS));
JTextField nameSUTextField = new JTextField();
nameSUTextField.setToolTipText("Name");
nameSUTextField.setMaximumSize(new Dimension(120, 20));
nameSUTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
nameSUTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
JTextField userNameSUTextField = new JTextField();
userNameSUTextField.setToolTipText("Username");
userNameSUTextField.setMaximumSize(new Dimension(120, 20));
userNameSUTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
userNameSUTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
JTextField passwordSUTextField = new JTextField();
passwordSUTextField.setMaximumSize(new Dimension(120, 20));
passwordSUTextField.setToolTipText("Password");
passwordSUTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
passwordSUTextField.setAlignmentY(Component.CENTER_ALIGNMENT);
JButton signUpJButton = new JButton("Sign up");
signUpJButton.setAlignmentX(Component.CENTER_ALIGNMENT);
signUpJButton.setAlignmentY(Component.CENTER_ALIGNMENT);
signUpJButton.addActionListener(event -> {
if (nameSUTextField.getText().length() > 0
&& userNameSUTextField.getText().length() > 0
&& passwordSUTextField.getText().length() > 0) {
if (database.get(userNameSUTextField.getText()) == null
|| !database.get(userNameSUTextField.getText()).equals(passwordSUTextField.getText())) {
JOptionPane.showMessageDialog(signUpFrame,
nameSUTextField.getText() + " has successfully registered " +
"and store corresponding username, password in the database", "Success",
JOptionPane.INFORMATION_MESSAGE);
database.put(userNameSUTextField.getText(),passwordSUTextField.getText());
} else {
JOptionPane.showMessageDialog(signUpFrame,
nameSUTextField.getText() + " already exists in the system", "Fail",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
signUpPanel.add(Box.createGlue());
signUpPanel.add(nameSUTextField);
signUpPanel.add(Box.createRigidArea(new Dimension(0, 10)));
signUpPanel.add(userNameSUTextField);
signUpPanel.add(Box.createRigidArea(new Dimension(0, 10)));
signUpPanel.add(passwordSUTextField);
signUpPanel.add(Box.createRigidArea(new Dimension(0, 10)));
signUpPanel.add(signUpJButton);
signUpPanel.add(Box.createGlue());
signUpFrame.add(signUpPanel);
signUpFrame.setSize(320, 250);
signUpFrame.setResizable(false);
signUpFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JFrame mainFrame = new JFrame("Main");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JButton signInMainJButton = new JButton("Sign in");
signInMainJButton.setAlignmentX(Component.CENTER_ALIGNMENT);
signInMainJButton.setAlignmentY(Component.CENTER_ALIGNMENT);
signInMainJButton.addActionListener(event -> {
userNameSITextField.setText("");
passwordSITextField.setText("");
signInFrame.setVisible(true);
});
JButton signUpMainJButton = new JButton("Sign up");
signUpMainJButton.setAlignmentX(Component.CENTER_ALIGNMENT);
signUpMainJButton.setAlignmentY(Component.CENTER_ALIGNMENT);
signUpMainJButton.addActionListener(event -> {
nameSUTextField.setText("");
userNameSUTextField.setText("");
passwordSUTextField.setText("");
signUpFrame.setVisible(true);
});
mainPanel.add(Box.createGlue());
mainPanel.add(signInMainJButton);
mainPanel.add(Box.createGlue());
mainPanel.add(signUpMainJButton);
mainPanel.add(Box.createGlue());
mainFrame.add(mainPanel);
mainFrame.setSize(320, 250);
mainFrame.setResizable(false);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Comments
Leave a comment