ashir_Qureshi

code_2

Jan 25th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1.  
  2. package com.currencycal;
  3.  
  4. import java.awt.GridLayout;
  5. import java.awt.HeadlessException;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.util.Scanner;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFileChooser;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPasswordField;
  19. import javax.swing.JTextArea;
  20.  
  21.  
  22. public class Login extends JFrame implements ActionListener{
  23. private JTextArea txtUsr;
  24. private JPasswordField txtPass;
  25. private JButton btnLogin;
  26. private JButton newUserName;
  27.  
  28.  
  29. public Login() throws HeadlessException {
  30.  
  31.  
  32. // defining text areas and buttons
  33. txtUsr = new JTextArea(); //username text area
  34.  
  35. txtPass = new JPasswordField();//password text area
  36.  
  37. btnLogin = new JButton("Login");//button "Login"
  38.  
  39. newUserName = new JButton("Sign UP");
  40. //defining ends
  41.  
  42. // Making Layout
  43. GridLayout grid = new GridLayout(4,1);
  44. setBounds(0, 0, 200, 200);
  45. setLayout(grid);
  46. // layout made above 3 lines
  47.  
  48. //drawing and labeling text and button on Frame
  49. add(new JLabel("User Name: "));
  50. add(txtUsr);
  51. add(new JLabel("Password: "));
  52. add(txtPass);
  53.  
  54. add(btnLogin); //adding button on screen
  55. add(newUserName); // adding signup button
  56. //frame exits on close button
  57. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.  
  59. //making login button into action
  60. btnLogin.addActionListener(this);
  61.  
  62. //signup button details
  63. newUserName.addActionListener(new ActionListener() {
  64. public void actionPerformed(ActionEvent e) {
  65. new SignUp().show();
  66. setVisible(false);
  67. dispose();
  68.  
  69. }
  70. });
  71. }
  72.  
  73. @Override
  74. public void actionPerformed(ActionEvent e) {
  75.  
  76. String textUser = txtUsr.getText();
  77. String textPass = txtPass.getText();
  78.  
  79. File f1 = new File("newUser.txt");
  80. if(!f1.exists())
  81. {
  82. System.out.println("File not FOund");
  83. }
  84.  
  85.  
  86. String dataPass2 = "";
  87. Scanner S = null;
  88. try {
  89. S = new Scanner(f1);
  90. } catch (FileNotFoundException e1) {
  91. // TODO Auto-generated catch block
  92. e1.printStackTrace();
  93. }
  94. String name3 = "";
  95. String pass3 = "";
  96. while (S.hasNextLine()) {
  97. dataPass2 = S.nextLine();
  98.  
  99. String[] parts = dataPass2.split("-", 2);
  100. name3 = parts[0];
  101. pass3 = parts[1];
  102.  
  103. //System.out.println("this is "+name3);
  104. //System.out.println("and"+pass3);
  105.  
  106.  
  107.  
  108.  
  109. //comparing with previous record
  110. if(textUser.equals("")&&textPass.equals(""))
  111. {
  112.  
  113. JOptionPane.showMessageDialog(null,"Please insert UserName and Password");
  114. break;
  115. }
  116. else if(!textUser.equals(name3) && !textPass.equals(pass3))
  117. {
  118.  
  119. JOptionPane.showMessageDialog(null,"wrong user name or password try again");
  120. break;
  121. }
  122.  
  123. else
  124. {
  125. name3 = pass3 = "";
  126. //graphic call
  127. new convertCurrency().show();
  128. this.setVisible(false);
  129. this.dispose();
  130. }
  131. } };
  132.  
  133. }
Add Comment
Please, Sign In to add comment