please someone teach me , based on the code given , how i can make the program run like the photo 1 , what password i have to key-in the program and make it run.
(Main java file)
package P3Q4;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*;
public class P3Q4 extends JFrame{
private JLabel jtaMessage=new JLabel(); private JLabel jtaText=new JLabel("Enter your password"); private JPasswordField jpfPassword = new JPasswordField(); private JButton jtnSubmit = new JButton("Submit"); private JLabel jtaEmpty = new JLabel(""); public P3Q4(){ JPanel passwordPanel= new JPanel(new GridLayout(2,2)); JPanel messagePanel= new JPanel(new FlowLayout(FlowLayout.LEFT,0,0)); messagePanel.setBackground(Color.WHITE); jtaText.setFont(new Font("Arial",Font.BOLD,14)); jtnSubmit.setFont(new Font("Arial",Font.BOLD,14)); jtaMessage.setFont(new Font("Arial",Font.BOLD,14)); jtaMessage.setVerticalAlignment(JLabel.TOP); jtaMessage.setPreferredSize(new Dimension(430,110)); passwordPanel.add(jtaText); passwordPanel.add(jpfPassword); passwordPanel.add(jtaEmpty); passwordPanel.add(jtnSubmit); messagePanel.add(jtaMessage); add(passwordPanel,BorderLayout.CENTER); add(messagePanel,BorderLayout.SOUTH); jtnSubmit.addActionListener(new ButtonListener()); setResizable(false); setTitle("Set Password"); setSize(400, 240); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private class ButtonListener implements ActionListener {
@Override public void actionPerformed(ActionEvent e) { String inputStr = new String(jpfPassword.getPassword()); try{ Password pw = new Password(inputStr); jtaMessage.setForeground(Color.BLUE); jtaMessage.setText("<html>Congratulations! Your password is valid. <html/>"); } catch(InvalidPasswordException ex){ jtaMessage.setForeground(Color.RED); jtaMessage.setText(ex.getMessage()); jpfPassword.grabFocus(); } } } public static void main(String[] args) { new P3Q4(); } }
|
(InvalidPasswordException)
package P3Q4;
public class InvalidPasswordException extends Exception{
public InvalidPasswordException(){ super("Invalid Password Exception"); } public InvalidPasswordException(String errMsg){ super(errMsg); } }
|
(Password)
package P3Q4;
public class Password {
public Password (String passwordStr) throws InvalidPasswordException { if (passwordStr== null ||passwordStr.equals("")) { throw new InvalidPasswordException("<html>Invalid password: password cannot be null or an empty string therefore please key-in the correct password.</html>"); } else { int countDigits=0,countLetters=0,total=0; for(int i=0;i<passwordStr.length();i++){ char ch = passwordStr.charAt(i); if (Character.isDigit(ch)){ countDigits++; } else if (Character.isLetter(ch)) countLetters++; } total = countDigits + countDigits ; String errMsg = "<html>"; boolean errorState=false; if (countDigits == 0){ errMsg += "Your password should have at least 1 digit.<br>"; errorState=true; } if (countLetters == 0){ errMsg += "Your password should have at least 1 letter.<br>"; errorState=true; } if (total<7){ errMsg += "Your password should have at least 7 alpha-numeric characters.<br>"; errorState=true; } if (errorState==true){ errMsg += "<html/>"; throw new InvalidPasswordException(errMsg); } } } }
|