Advertisement
Guest User

Untitled

a guest
Jan 4th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.border.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.*;
  7. public class MyLogin extends JFrame implements ActionListener {
  8. private JPanel p1, p2;
  9. private JLabel l1, l2, l3;
  10. private JTextField username;
  11. private JPasswordField password;
  12. private JButton ok, cancel;
  13. private JCheckBox tick;
  14. private String filename;
  15. private HashMap<String, User> table;
  16. public MyLogin() {
  17. super("Регистрация");
  18. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. Container container = this.getContentPane();
  20. p1 = new JPanel();
  21. p1.setBorder(new EtchedBorder());
  22. p1.setLayout(new GridLayout(3, 3));
  23. l1 = new JLabel("Потребителско име");
  24. p1.add(l1);
  25. username = new JTextField(20);
  26. p1.add(username);
  27. l2 = new JLabel("Парола");
  28. p1.add(l2);
  29. password = new JPasswordField(20);
  30. p1.add(password);
  31. l3 = new JLabel("Нов потребител");
  32. p1.add(
  33. l3);
  34. tick = new JCheckBox();
  35. p1.add(tick);
  36. container.add(p1, BorderLayout.CENTER);
  37. p2 = new JPanel();
  38. p2.setBorder(new EtchedBorder());
  39. ok = new JButton("Потвърди");
  40. ok.addActionListener(this);
  41. p2.add(ok);
  42. cancel = new JButton("Отмени");
  43. cancel.addActionListener(this);
  44. p2.add(cancel);
  45. container.add(p2, BorderLayout.PAGE_END);
  46. filename = "users.txt";
  47. table = new HashMap();
  48. readFile();
  49. this.setSize(280, 150);
  50. this.setVisible(true);
  51. }
  52. private void readFile() {
  53. FileReader fr = null;
  54. BufferedReader in = null;
  55. String line = null;
  56. String[] split;
  57. User user = null;
  58. try {
  59. fr = new FileReader(filename);
  60. in = new BufferedReader(fr);
  61. table = new HashMap();
  62. while((line = in.readLine()) != null) {
  63. split = line.split("\\t");
  64. user =
  65. new User(split[0], split[1]);
  66. table.put(user.getName(), user);
  67. }
  68. }
  69. catch (FileNotFoundException fnfe) {
  70. JOptionPane.showMessageDialog(this, "Файлът " + filename + " не е намерен!");
  71. }
  72. catch (IOException ioe) {
  73. JOptionPane.showMessageDialog(this, "Входно/изходна грешка при четене от файла " + filename);
  74. }
  75. finally {
  76. if(in != null) {
  77. try {
  78. in.close();
  79. }
  80. catch (IOException ioe) {
  81. JOptionPane.showMessageDialog(this, "Грешка при затваряне на файла " + filename);
  82. }
  83. }
  84. }
  85. }
  86. private void writeFile() {
  87. FileWriter fw = null;
  88. BufferedWriter bw = null;
  89. PrintWriter out = null;
  90. try {
  91. fw = new FileWriter(filename);
  92. bw = new BufferedWriter(fw);
  93. out = new PrintWriter(bw);
  94. for (Map.Entry<String, User> entry : table.entrySet())
  95. out.println(entry.getValue());
  96. }
  97. catch(IOException ioe) {
  98. JOptionPane.showMessageDialog(this, "Грешка при запис във файла " + filename);
  99. }
  100. finally {
  101. if(out != null) {
  102. out.close();
  103. }
  104. }
  105. }
  106. public void actionPerformed(ActionEvent e) {
  107. Object source = e.getSource();
  108. User user = null, find = null;
  109. String key = null, name = null, pass = null;
  110. if(source == ok) {
  111. name = username.getText();
  112. pass = new String(password.getPassword());
  113. if(name.length() == 0 && pass.length() == 0) {
  114. JOptionPane.showMessageDialog(null,
  115. "Въведете потребителско име и парола!","Съобщение",
  116. JOptionPane.INFORMATION_MESSAGE);
  117. }
  118. else {
  119. user = new User(name, pass);
  120. key = user.getName();
  121. if(tick.isSelected()) {
  122. if(table.containsKey(key)) {
  123. JOptionPane.showMessageDialog(null,
  124. "В системата съществува потребител с име " + user.getName(),
  125. "Съобщение", JOptionPane.INFORMATION_MESSAGE);
  126. }
  127. else {
  128. table.put(key, user);
  129. writeFile();
  130. new Editor(key);
  131. }
  132. }
  133. else {
  134. if(table.containsKey(key)) {
  135. find = table.get(key);
  136. if(user.getPassword().equals(find.getPassword()))
  137. new Editor(key);
  138. else
  139. JOptionPane.showMessageDialog(null, "Грешна парола!", "Съобщение",
  140. JOptionPane.INFORMATION_MESSAGE);
  141. }
  142. else
  143. JOptionPane.showMessageDialog(null,
  144. "Грешно потребителско име!", "Съобщение",
  145. JOptionPane.INFORMATION_MESSAGE);
  146. }
  147. }
  148. }
  149. else if(source == cancel) {
  150. username.
  151. setText("");
  152. password.setText("");
  153. tick.setSelected(false);
  154. }
  155. }
  156. public static void main(String[] args) {
  157. SwingUtilities.invokeLater(new Runnable() {
  158. public void run() {
  159. new MyLogin();
  160. }
  161. });
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement