Source of UsersFrame.java


  1: import javax.swing.*;
  2: import java.awt.*;
  3: import java.awt.event.*;
  4: 
  5: public class UsersFrame extends JFrame
  6:         implements ActionListener
  7: {
  8:         public static final int WIDTH = 400;
  9:         public static final int HEIGHT = 300;
 10: 
 11:         private JLabel userLabel;
 12:         private JLabel pwdLabel;
 13:         private JTextField userTextField;
 14:         private JTextField passwordTextField;
 15:         private JButton signinButton;
 16:         private JButton cancelButton;
 17: 
 18:         public UsersFrame()
 19:         {
 20:                 setSize(WIDTH, HEIGHT);
 21:                 WindowDestroyer listener = new WindowDestroyer();
 22:                 addWindowListener(listener);
 23: 
 24:                 Container contentPane = getContentPane();
 25:                 contentPane.setLayout(new FlowLayout());
 26: 
 27:                 //User name components
 28:                 userLabel = new JLabel("User Name: ");
 29:                 contentPane.add(userLabel);
 30:                 userTextField = new JTextField(30);
 31:                 contentPane.add(userTextField);
 32: 
 33:                 //Password components
 34:                 pwdLabel = new JLabel("Password: ");
 35:                 contentPane.add(pwdLabel);
 36:                 passwordTextField = new JTextField(30);
 37:                 contentPane.add(passwordTextField);
 38: 
 39:                 //Buttons
 40:                 signinButton = new JButton("Sign In Now!");
 41:                 contentPane.add(signinButton);
 42:                 signinButton.addActionListener(this);
 43: 
 44:                 cancelButton = new JButton("Cancel");
 45:                 contentPane.add(cancelButton);
 46:                 cancelButton.addActionListener(this);
 47:         }
 48: 
 49:         public void actionPerformed(ActionEvent e)
 50:         {
 51:                 String actionCommand = e.getActionCommand();
 52: 
 53:                 if(actionCommand.equals("Sign In Now!"))
 54:                 {
 55:                         try
 56:                         {
 57:                                 String password = passwordTextField.getText();
 58:                                 if(!checkPasswordLength(password))
 59:                                 {
 60:                                         throw new ShortPasswordException();
 61:                                 }
 62:                                 else
 63:                                 {
 64:                                         JOptionPane.showMessageDialog(null,
 65:                                                 "User name " + userTextField.getText() +
 66:                                                 "\nPassword " + passwordTextField.getText());
 67:                                         userTextField.setText(" ");
 68:                                         passwordTextField.setText(" ");
 69:                                 }
 70:                         }
 71:                         catch(ShortPasswordException spe)
 72:                         {
 73:                                 JOptionPane.showMessageDialog(null, spe.getMessage());
 74:                         }
 75:                 }
 76:                 else if(actionCommand.equals("Cancel"))
 77:                 {
 78:                         userTextField.setText(" ");
 79:                         passwordTextField.setText(" ");
 80:                 }
 81:         }
 82: 
 83:         public boolean checkPasswordLength(String pwd) throws
 84:                 ShortPasswordException
 85:         {
 86:                 if(pwd.length() >= 8)
 87:                         return true;
 88:                 else
 89:                         return false;
 90:         }
 91: 
 92:         public static void main(String[] args)
 93:         {
 94:                 UsersFrame gui = new UsersFrame();
 95:                 gui.setVisible(true);
 96:         }
 97: }