Question: When an invalid password is put in, please have a window that says invalid password that you can click okay and then retry instead of
When an invalid password is put in, please have a window that says invalid password that you can click okay and then retry instead of kicking you out of the program. Also please comment all the code if possible.
LoginAccount.java
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class LoginAccountGUI { private static JFrame mainFrame; private static JPanel mainPanel, buttonPanel; private static JButton createButton, loginButton, cancelButton; public static void main(String[] args) { Account account = new Account(); mainFrame = new JFrame("Account Creation and Login"); mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); createButton = new JButton("Create Account"); loginButton = new JButton("Login"); cancelButton = new JButton("Cancel"); buttonPanel.add(createButton); buttonPanel.add(loginButton); buttonPanel.add(cancelButton); mainPanel.add(buttonPanel); mainFrame.add(mainPanel); mainFrame.setSize(300, 100); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setLocationRelativeTo(null); mainFrame.setVisible(true); // action listeners for the buttons createButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String userName = JOptionPane.showInputDialog(null, "Please enter a username:").trim(); String password = JOptionPane.showInputDialog(null, "Please enter a password " + "Password must at least be 9 characters long and must contain at least: " + " -- one uppercase letter " + " -- one lowercase letter " + " -- one digit").trim(); account.setUserName(userName); account.setPassword(password); JOptionPane.showMessageDialog(null, "Account created successfully!"); }catch(NullPointerException npe){ JOptionPane.showMessageDialog(null, "All fields are mandatory. Please try again!"); System.exit(-1); } } }); loginButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(account.getUserName().equals("") || account.getPassword().equals("")) { JOptionPane.showMessageDialog(null, "Please create an account to login!"); return; } try { String originalUserName = account.getUserName().trim(); String originalPassword = account.getPassword().trim(); String userNameEntered = JOptionPane.showInputDialog(null, "Please enter the username:").trim(); String passwordEntered = JOptionPane.showInputDialog(null, "Please enter the password:").trim();
if(originalUserName.equals(userNameEntered) && originalPassword.equals(passwordEntered)) { JOptionPane.showMessageDialog(null, "Congratulations! You logged in successfully."); new FrameAccount();
//simply calling default constructor of FrameAccount }else { JOptionPane.showMessageDialog(null, "Sorry! Please check your username/password."); } }catch(NullPointerException npe){ JOptionPane.showMessageDialog(null, "All fields are mandatory. Please try again!"); System.exit(-1); } } }); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } }
Account.java ---
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Account { private String userName, password; private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{9})"; public Account() { this.userName = ""; this.password = ""; } public Account(String userName, String password) { Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); if(!matcher.matches()) { System.out.println("Invalid pattern for password detected!"); System.exit(-1); } this.userName = userName; this.password = password; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassword() { return password; }
public void setPassword(String password) { Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); if(!matcher.matches()) { System.out.println("Invalid pattern for password detected!"); System.exit(-1); } this.password = password; } @Override public String toString() { return ("Username: " + this.userName + ", Password: " + this.password); } }
FrameAccount.java ----
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class FrameAccount { //creating the class private static JFrame mainFrame; //static JFrame for Window Screen private static JButton loginButton, cancelButton; //Button to ok and cancelButton ; public FrameAccount(){ //use the default constructor to implement the code . mainFrame = new JFrame("Main Page :"); //creat frame and give name as MAin Page loginButton = new JButton("Ok"); cancelButton = new JButton("Cancel"); JLabel temp,wind,dew,windchill,cloudbase; /* here we using the swing JLabel and JTextField to get the input and JLabel show the what actually the field want to get the value and then setBounds each Label and TextField on the window screen */ temp=new JLabel("Enter temperature (in degress) : "); wind=new JLabel("Enter wind speed (in mph) : "); dew=new JLabel("Enter dew Point : "); windchill=new JLabel("Enter wind chill : "); cloudbase=new JLabel("Enter cloud Base : "); JTextField temptext,windtext,dewtext,windchilltext,cloudbasetext; temptext=new JTextField(); windtext=new JTextField(); dewtext=new JTextField(); windchilltext=new JTextField(); cloudbasetext=new JTextField();
temp.setBounds(10,10,200,30); temptext.setBounds(220,10,100,30);
wind.setBounds(10,50,200,30); windtext.setBounds(220,50,100,30);
dew.setBounds(10,100,200,30); dewtext.setBounds(220,100,100,30);
windchill.setBounds(10,150,200,30); windchilltext.setBounds(220,150,100,30);
cloudbase.setBounds(10,200,200,30); cloudbasetext.setBounds(220,200,100,30);
mainFrame.setBounds(50,50,500,500); loginButton.setBounds(20,270,100,25); cancelButton.setBounds(140,270,100,25);
//now add each component to JFrame to show the component on the window
mainFrame.add(temp); mainFrame.add(temptext); mainFrame.add(wind); mainFrame.add(windtext); mainFrame.add(dew); mainFrame.add(dewtext); mainFrame.add(windchill); mainFrame.add(windchilltext); mainFrame.add(cloudbase); mainFrame.add(cloudbasetext);
//also add buttons mainFrame.add(loginButton); mainFrame.add(cancelButton);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setLocationRelativeTo(null); mainFrame.setLayout(null); mainFrame.setVisible(true); // action listeners for the buttons
loginButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //here you can put your code . what you actually want from the code ...
}}); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { //calling default constructor new FrameAccount(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
