Advertisement
Guest User

Untitled

a guest
May 26th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class login extends JFrame {
  8. JButton blogin;
  9. JPanel loginpanel;
  10. JTextField txuser;
  11. JTextField pass;
  12. JButton newUSer;
  13. JLabel username;
  14. JLabel password;
  15.  
  16.  
  17. public login() {
  18. super("Login Autentification");
  19.  
  20. blogin = new JButton("Login");
  21. loginpanel = new JPanel();
  22. txuser = new JTextField(15);
  23. pass = new JPasswordField(15);
  24. newUSer = new JButton("New User?");
  25. username = new JLabel("User - ");
  26. password = new JLabel("Pass - ");
  27.  
  28. setSize(300, 200);
  29. setLocation(500, 280);
  30. loginpanel.setLayout(null);
  31.  
  32.  
  33. txuser.setBounds(70, 30, 150, 20);
  34. pass.setBounds(70, 65, 150, 20);
  35. blogin.setBounds(110, 100, 80, 20);
  36. newUSer.setBounds(110, 135, 80, 20);
  37. username.setBounds(20, 28, 80, 20);
  38. password.setBounds(20, 63, 80, 20);
  39.  
  40. loginpanel.add(blogin);
  41. loginpanel.add(txuser);
  42. loginpanel.add(pass);
  43. loginpanel.add(newUSer);
  44. loginpanel.add(username);
  45. loginpanel.add(password);
  46.  
  47. getContentPane().add(loginpanel);
  48. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49. setVisible(true);
  50.  
  51. Writer writer = null;
  52. File check = new File("userPass.txt");
  53. if (check.exists()) {
  54.  
  55. //Checks if the file exists. will not add anything if the file does exist.
  56. } else {
  57. try {
  58. File texting = new File("userPass.txt");
  59. writer = new BufferedWriter(new FileWriter(texting));
  60. writer.write("message");
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66.  
  67. blogin.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. try {
  70. File file = new File("userPass.txt");
  71. Scanner scan = new Scanner(file);
  72. ;
  73. String line = null;
  74. FileWriter filewrite = new FileWriter(file, true);
  75.  
  76. String usertxt = " ";
  77. String passtxt = " ";
  78. String puname = txuser.getText();
  79. String ppaswd = pass.getText();
  80.  
  81.  
  82. while (scan.hasNext()) {
  83. if (puname.equals(usertxt) && ppaswd.equals(passtxt)) {
  84. MainMenu menu = new MainMenu();
  85. dispose();
  86. }
  87.  
  88. usertxt = scan.nextLine();
  89. passtxt = scan.nextLine();
  90. }
  91.  
  92. else if(puname.equals("") && ppaswd.equals("")) {
  93. JOptionPane.showMessageDialog(null, "Please insert Username and Password");
  94. }
  95. else{
  96.  
  97. JOptionPane.showMessageDialog(null, "Wrong Username / Password");
  98. txuser.setText("");
  99. pass.setText("");
  100. txuser.requestFocus();
  101. }
  102. } catch (IOException d) {
  103. d.printStackTrace();
  104. }
  105.  
  106. }
  107. });
  108.  
  109. newUSer.addActionListener(new ActionListener() {
  110. public void actionPerformed(ActionEvent e) {
  111. NewUser user = new NewUser();
  112. dispose();
  113.  
  114. }
  115. });
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement