Guest User

Untitled

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