Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. package student_manager;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.BoxLayout;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JTextField;
  13. import java.awt.Color;
  14. import java.awt.Component;
  15. import java.awt.Font;
  16. import javax.swing.SwingConstants;
  17. import javax.swing.JPasswordField;
  18. import javax.swing.JButton;
  19. import java.awt.event.ActionListener;
  20. import java.util.Arrays;
  21. import java.awt.event.ActionEvent;
  22. import javax.swing.JSeparator;
  23. import javax.swing.UIManager;
  24.  
  25. public class LoginWindow extends JFrame implements ActionListener{
  26.  
  27. public static LoginWindow loginFrame = new LoginWindow();
  28.  
  29.  
  30. private JPanel contentPane;
  31. private JTextField tfUsername;
  32. private JPasswordField passwordField;
  33. private JButton btnLogin;
  34. private JButton btnCancel;
  35.  
  36. /**
  37. * Launch the application.
  38. */
  39. public static void main(String[] args) {
  40.  
  41. EventQueue.invokeLater(new Runnable()
  42. {
  43. public void run()
  44. {
  45. try
  46. {
  47.  
  48. loginFrame.setVisible(true);
  49. }
  50. catch (Exception e)
  51. {
  52. e.printStackTrace();
  53. }
  54. }
  55. });
  56. }
  57.  
  58.  
  59. /**
  60. * Create the frame.
  61. */
  62. public LoginWindow() {
  63. setTitle("Student Manager - Log in");
  64. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65. setBounds(100, 100, 450, 300);
  66. contentPane = new JPanel();
  67. contentPane.setBackground(new Color(51, 153, 204));
  68. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  69. setContentPane(contentPane);
  70. contentPane.setLayout(null);
  71.  
  72. JLabel lblWelcome = new JLabel("Welcome to Student Manager,");
  73. lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
  74. lblWelcome.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 23));
  75. lblWelcome.setBounds(59, 11, 328, 23);
  76. contentPane.add(lblWelcome);
  77.  
  78. JLabel lblUsername = new JLabel("Username");
  79. lblUsername.setForeground(new Color(51, 51, 51));
  80. lblUsername.setFont(new Font("Arial", Font.BOLD, 18));
  81. lblUsername.setBounds(86, 88, 89, 17);
  82. contentPane.add(lblUsername);
  83.  
  84. JLabel lblPassword = new JLabel("Password");
  85. lblPassword.setForeground(new Color(51, 51, 51));
  86. lblPassword.setFont(new Font("Arial", Font.BOLD, 18));
  87. lblPassword.setBounds(86, 132, 89, 17);
  88. contentPane.add(lblPassword);
  89.  
  90. JLabel lblNewLabel_1 = new JLabel("Please log in to continue!");
  91. lblNewLabel_1.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 23));
  92. lblNewLabel_1.setBounds(86, 35, 279, 26);
  93. contentPane.add(lblNewLabel_1);
  94.  
  95. tfUsername = new JTextField();
  96. tfUsername.setFont(UIManager.getFont("TextField.font"));
  97. tfUsername.setBounds(185, 85, 120, 23);
  98. contentPane.add(tfUsername);
  99. tfUsername.setColumns(10);
  100.  
  101. passwordField = new JPasswordField();
  102. passwordField.setFont(UIManager.getFont("PasswordField.font"));
  103. passwordField.setBounds(185, 129, 120, 23);
  104. contentPane.add(passwordField);
  105.  
  106. btnLogin = new JButton("LOG IN");
  107. btnLogin.setBackground(new Color(204, 204, 204));
  108. btnLogin.setFont(new Font("Arial", Font.BOLD, 14));
  109. btnLogin.setBounds(86, 195, 89, 36);
  110. contentPane.add(btnLogin);
  111. btnLogin.addActionListener(this);
  112.  
  113. btnCancel = new JButton("CANCEL");
  114. btnCancel.setBackground(new Color(204, 204, 204));
  115. btnCancel.setFont(new Font("Arial", Font.BOLD, 14));
  116. btnCancel.setBounds(276, 195, 89, 36);
  117. contentPane.add(btnCancel);
  118. btnCancel.addActionListener(this);
  119.  
  120. JSeparator separator1 = new JSeparator();
  121. separator1.setForeground(new Color(0, 51, 102));
  122. separator1.setToolTipText("");
  123. separator1.setBackground(new Color(0, 51, 102));
  124. separator1.setBounds(10, 67, 414, 9);
  125. contentPane.add(separator1);
  126.  
  127. JSeparator separator2 = new JSeparator();
  128. separator2.setForeground(new Color(0, 51, 102));
  129. separator2.setBackground(new Color(0, 51, 102));
  130. separator2.setBounds(10, 174, 414, 9);
  131. contentPane.add(separator2);
  132. }
  133.  
  134. private static boolean isPasswordCorrect(char[] input) {
  135. boolean isCorrect = true;
  136. char[] correctPassword = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };
  137.  
  138. if (input.length != correctPassword.length) {
  139. isCorrect = false;
  140. } else {
  141. isCorrect = Arrays.equals (input, correctPassword);
  142. }
  143.  
  144. //Zero out the password.
  145. Arrays.fill(correctPassword,'0');
  146.  
  147. return isCorrect;
  148. }
  149.  
  150. @Override
  151. public void actionPerformed(ActionEvent e) {
  152. char[] input = passwordField.getPassword();
  153. if (e.getSource() == btnLogin){
  154. if (tfUsername.getText().equals("Admin") && isPasswordCorrect(input)){
  155. loginFrame.setVisible( false );
  156. CreateRecords.createFrame.setVisible(true);
  157. }
  158. else {
  159. JOptionPane.showMessageDialog(null, "Invalid username or password!");
  160. }
  161. }
  162. if (e.getSource() == btnCancel){
  163. System.exit(0);
  164. }
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement