Guest User

Untitled

a guest
Jan 4th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.util.Scanner;
  7. import java.util.StringTokenizer;
  8.  
  9.  
  10. public class loginsource extends JFrame
  11. {
  12. // Variables declaration
  13. private JLabel jLabel1;
  14. private JLabel jLabel2;
  15. private JTextField jTextField1;
  16. private JPasswordField jPasswordField1;
  17. private JButton jButton1;
  18. private JPanel contentPane;
  19. // End of variables declaration
  20.  
  21.  
  22. public loginsource()
  23. {
  24. super();
  25. create();
  26. this.setVisible(true);
  27. }
  28.  
  29.  
  30. private void create()
  31. {
  32. jLabel1 = new JLabel();
  33. jLabel2 = new JLabel();
  34. jTextField1 = new JTextField();
  35. jPasswordField1 = new JPasswordField();
  36. jButton1 = new JButton();
  37. contentPane = (JPanel)this.getContentPane();
  38.  
  39. //
  40. // jLabel1
  41. //
  42. jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
  43. jLabel1.setText("Username:");
  44. //
  45. // jLabel2
  46. //
  47. jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
  48. jLabel2.setText("Password:");
  49. //
  50. // jTextField1
  51. //
  52.  
  53. jTextField1.addActionListener(new ActionListener() {
  54. public void actionPerformed(ActionEvent e)
  55. {
  56. jTextField1_actionPerformed(e);
  57. }
  58.  
  59. });
  60. //
  61. // jPasswordField1
  62. //
  63. jPasswordField1.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e)
  65. {
  66. jPasswordField1_actionPerformed(e);
  67. }
  68.  
  69. });
  70. //
  71. // jButton1
  72. //
  73. jButton1.setBackground(new Color(204, 204, 204));
  74. jButton1.setText("Login");
  75. jButton1.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent e)
  77. {
  78. jButton1_actionPerformed(e);
  79. }
  80. });
  81. //
  82. // contentPane
  83. //
  84. contentPane.setLayout(null);
  85. contentPane.setBorder(BorderFactory.createEtchedBorder());
  86. contentPane.setBackground(new Color(204, 204, 204));
  87. addComponent(contentPane, jLabel1, 5,10,106,18);
  88. addComponent(contentPane, jLabel2, 5,47,97,18);
  89. addComponent(contentPane, jTextField1, 110,10,183,22);
  90. addComponent(contentPane, jPasswordField1, 110,45,183,22);
  91. addComponent(contentPane, jButton1, 135,75,83,28);
  92. //
  93. // login
  94. //
  95. this.setTitle("fail0verflow Login Window");
  96. this.setLocation(new Point(76, 182));
  97. this.setSize(new Dimension(335, 141));
  98. this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  99. this.setResizable(false);
  100. }
  101.  
  102. /** Add Component Without a Layout Manager (Absolute Positioning) */
  103. private void addComponent(Container container,Component c,int x,int y,int width,int height)
  104. {
  105. c.setBounds(x,y,width,height);
  106. container.add(c);
  107. }
  108.  
  109.  
  110. private void jTextField1_actionPerformed(ActionEvent e)
  111. {
  112.  
  113.  
  114. }
  115.  
  116. private void jPasswordField1_actionPerformed(ActionEvent e)
  117. {
  118.  
  119. }
  120.  
  121. private void jButton2_actionPerformed(ActionEvent e)
  122. {
  123.  
  124. }
  125.  
  126. private void jButton1_actionPerformed(ActionEvent e)
  127. {
  128. String username = new String(jTextField1.getText());
  129. String password = new String(jPasswordField1.getPassword());
  130.  
  131. if(username.equals("") || password.equals("")) // If password and username is empty > Do this >>>
  132. {
  133. jButton1.setEnabled(false);
  134. JOptionPane.showMessageDialog(null,"You must enter a username and password to login.");
  135. jTextField1.setText("");
  136. jPasswordField1.setText("");
  137. jButton1.setEnabled(true);
  138. this.setVisible(true);
  139. }
  140. else
  141. {
  142. int confirm =JOptionPane.showConfirmDialog(null,"<HTML>You entered<FONT COLOR = RED> <B>"+username+
  143. "</B></FONT> as your username.<BR> Do you want to continue?</HTML>");
  144. switch(confirm){ // Switch > Case
  145. case JOptionPane.YES_OPTION: // Attempt to Login user
  146. jButton1.setEnabled(false); // Set button enable to false to prevent 2 login attempts
  147. File file = new File("C:/text.txt");
  148. try{
  149. boolean u = false;
  150. boolean p = false;
  151. char[] pass = jPasswordField1.getPassword();
  152. String f = "";
  153. String c = new String(pass);
  154. Scanner s = new Scanner(file);
  155. while(s.hasNextLine()) {
  156. String[] a = s.nextLine().split(",");
  157. int x = 0;
  158. for(; x < a.length; x+=2)
  159. {
  160. if(username.equals(a[x]))
  161. {
  162. u = true;
  163. f = a[x];
  164. break;
  165. }
  166. else{continue;}
  167. }
  168. for(; x < a.length; x+=1)
  169. {
  170. if(c.equals(a[x]))
  171. {
  172. p = true;
  173. }
  174. }
  175. }
  176. if(u == true && p == true) {
  177. JOptionPane.showMessageDialog(null, "Login success.\nWelcome " + f +"!");
  178. jButton1.setEnabled(true);
  179. } else {
  180. JOptionPane.showMessageDialog(null, "Invalid Account!");
  181. jButton1.setEnabled(true);
  182. }
  183. }
  184. catch (Exception db)
  185. {
  186. JOptionPane.showMessageDialog(null, "Error reading database.\nProgram will now exit.", "Error", JOptionPane.ERROR_MESSAGE);
  187. System.exit(0);
  188. }
  189. this.setVisible(true);
  190. break;
  191.  
  192. case JOptionPane.NO_OPTION: // No Case.(Go back. Set text to 0)
  193. jButton1.setEnabled(false);
  194. jTextField1.setText("");
  195. jPasswordField1.setText("");
  196. jButton1.setEnabled(true);
  197. break;
  198.  
  199. case JOptionPane.CANCEL_OPTION: // Cancel Case.(Go back. Set text to 0)
  200. jButton1.setEnabled(false);
  201. jTextField1.setText("");
  202. jPasswordField1.setText("");
  203. jButton1.setEnabled(true);
  204. break;
  205.  
  206. } // End Switch > Case
  207.  
  208.  
  209. }
  210. }
  211. public static void main(String[] args)
  212. {
  213. JFrame.setDefaultLookAndFeelDecorated(true);
  214. JDialog.setDefaultLookAndFeelDecorated(true);
  215. try
  216. {
  217. UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  218. }
  219. catch (Exception ex)
  220. {
  221. System.out.println("Failed loading L&F: ");
  222. System.out.println(ex);
  223. }
  224. new loginsource();
  225. };
  226.  
  227.  
  228.  
  229. }
Add Comment
Please, Sign In to add comment