Guest User

Untitled

a guest
Jan 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8.  
  9. public class Password extends JFrame{
  10.  
  11. Container cp;
  12. JPanel panelPassword;
  13. JPanel panelUser;
  14. JPanel panelExit;
  15. JPanel panelHelp, panelLog;
  16. JPasswordField passWord;
  17. JPasswordField user;
  18. JTextField text;
  19. JTextField textU;
  20. JButton exitButton, helpButton, logButton;
  21. JLabel label, labelU;
  22. int x=3, xP=2;
  23.  
  24.  
  25. public Password (String title)
  26. {
  27. setSize(650,150);
  28. setTitle(title);
  29. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.  
  31. //set up the container
  32. cp = getContentPane();
  33.  
  34.  
  35.  
  36. user = new JPasswordField(10);   //USER PANEL
  37. //passWord.setActionCommand("OK");
  38. user.setEchoChar('*');
  39. user.addActionListener(new ButtonWatcher());
  40. labelU = new JLabel("Please enter your Username: ");
  41. labelU.setLabelFor(user);
  42.  
  43. textU = new JTextField();
  44. textU.add(user);  //END USER PANEL
  45.  
  46. passWord = new JPasswordField(10);  //PASSWORD PANEL
  47. //passWord.setActionCommand("OK");
  48. passWord.setEchoChar('*');
  49. passWord.addActionListener(new ButtonWatcher());
  50. label = new JLabel("Enter the password: ");
  51. label.setLabelFor(passWord);
  52.  
  53. text = new JTextField();
  54. text.add(passWord);  //END PASSWORD PANEL
  55.  
  56. //create the buttons
  57.  
  58. exitButton = new JButton("Exit");  //EXIT BUTTON
  59. exitButton.addActionListener(new ButtonWatcher());
  60.  
  61. helpButton = new JButton("Help"); //HELP BUTTON
  62. helpButton.addActionListener(new ButtonWatcher());
  63.  
  64. logButton = new JButton("Log In"); //LOGIN IN BUTTON
  65. logButton.addActionListener(new ButtonWatcher());
  66.  
  67. //create the panel and add the passWord field and buttons
  68.  
  69. panelPassword = new JPanel();
  70. panelPassword.setLayout(new FlowLayout());
  71. panelPassword.add(label);
  72. panelPassword.add(passWord);
  73.  
  74. panelUser = new JPanel();
  75. panelUser.setLayout(new FlowLayout());
  76. panelUser.add(labelU);
  77. panelUser.add(user);
  78.  
  79. panelExit = new JPanel();
  80. panelExit.setLayout(new GridLayout(1,3));
  81. panelExit.add(exitButton);
  82. panelExit.add(helpButton);
  83. panelExit.add(logButton);
  84.  
  85. //add the panels to the container
  86.  
  87. cp.add(panelPassword,BorderLayout.CENTER);
  88. cp.add(panelUser,BorderLayout.WEST);
  89. cp.add(panelExit,BorderLayout.SOUTH);
  90.  
  91.  
  92. }
  93. //create an inner class to 'listen' to the buttons
  94.  
  95. private class ButtonWatcher implements ActionListener
  96. {
  97. public void actionPerformed(ActionEvent event)
  98. {
  99. String cmd = event.getActionCommand();
  100. Object ob = event.getSource();
  101.  
  102.  
  103. {
  104.     if(ob.equals(logButton)  || ob.equals(passWord))
  105.  
  106.    
  107.  
  108.         if( cmd.equalsIgnoreCase("grob") )
  109.      
  110.  
  111.             JOptionPane.showMessageDialog(null, "Priority 1, Logout with typing in end as the password");
  112.    
  113.  
  114.         else if (cmd.equalsIgnoreCase("muehler") )
  115.      
  116.  
  117.             JOptionPane.showMessageDialog(null, "Priority 2, Logout with typing in end as the password");
  118.  
  119.         else if(cmd.equalsIgnoreCase("alex") )
  120.      
  121.  
  122.             JOptionPane.showMessageDialog(null, "Priority 3, Logout with typing in end as the password");
  123.  
  124.  
  125.         else if(cmd.equalsIgnoreCase("end"))
  126.         { //END
  127.             JOptionPane.showMessageDialog(null, "System shutting down");
  128.  
  129.             System.exit(0); } //END
  130.  
  131.  
  132.  
  133.         else  { //ERROR
  134.             JOptionPane.showMessageDialog(null,
  135.                     "Invalid username or password. "+ xP +" try left",
  136.                     "Error Message",
  137.  
  138.                     JOptionPane.ERROR_MESSAGE);
  139.             x=x-1;
  140.             } //ERROR
  141.  
  142.     if(x==3)
  143.     xP=2;
  144.  
  145.     if (x==2)
  146.     xP=1;
  147.  
  148.     if(x==1)
  149.     xP=0;
  150.  
  151.     if(x==0) {
  152.     JOptionPane.showMessageDialog(null, "You have enter the wrong password three times. The programm will quit");
  153.    
  154.     System.exit(0);
  155.     }
  156.  
  157. }// IF CLASS END
  158.  
  159.  
  160.     if(ob.equals(helpButton))
  161.     JOptionPane.showMessageDialog(null, "HELP, Please enter your username and password for your user account.You have "+ x +" tries to enter your password and username");
  162.     {  
  163.         if(ob.equals(exitButton))
  164.         {
  165.    
  166.    
  167.  
  168.             setVisible(false);//close the current window
  169.             System.exit(0);//exit the system
  170.  
  171.  
  172.  
  173. }//end actionPerformed
  174. }//end ButtonWatcher class
  175.  
  176. }
  177. }
  178. }
Add Comment
Please, Sign In to add comment