Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. package passwordProgram;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.util.Arrays;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21. import javax.swing.UIManager;
  22.  
  23. public class CreateAccount implements ActionListener {
  24.  
  25. JFrame frame;
  26. JPanel panel;
  27. JTextField username;
  28. JPasswordField password;
  29. JPasswordField confirmPassword;
  30. JLabel warningLabel;
  31.  
  32. public static void main(String[] args) {
  33. try {
  34. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. CreateAccount window = new CreateAccount();
  39. window.go();
  40. }
  41.  
  42. public void go() {
  43. frame = new JFrame("Create a new account");
  44. panel = new JPanel();
  45.  
  46. panel.setLayout(new GridBagLayout());
  47.  
  48. frame.getContentPane().add(BorderLayout.CENTER, panel);
  49. panel.setBackground(Color.ORANGE);
  50.  
  51. JLabel userLabel = new JLabel("Username:");
  52. JLabel passwordLabel = new JLabel("Password:");
  53. JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
  54. username = new JTextField(15);
  55. password = new JPasswordField(15);
  56. confirmPassword = new JPasswordField(15);
  57.  
  58. GridBagConstraints right = new GridBagConstraints();
  59. right.anchor = GridBagConstraints.WEST;
  60. GridBagConstraints left = new GridBagConstraints();
  61. left.anchor = GridBagConstraints.EAST;
  62.  
  63. right.weightx = (int) 2;
  64. right.fill = GridBagConstraints.REMAINDER;
  65. right.gridwidth = GridBagConstraints.REMAINDER;
  66. // actual GUI
  67.  
  68. panel.add(userLabel, left);
  69. panel.add(username, right);
  70. panel.add(passwordLabel, left);
  71. panel.add(password, right);
  72. panel.add(confirmPasswordLabel, left);
  73. panel.add(confirmPassword, right);
  74.  
  75. frame.setSize(300, 250);
  76. frame.setVisible(true);
  77.  
  78. JButton createAccount = new JButton("Create this account");
  79. frame.getContentPane().add(BorderLayout.SOUTH, createAccount);
  80. createAccount.addActionListener(this);
  81.  
  82. warningLabel = new JLabel();
  83. frame.getContentPane().add(BorderLayout.NORTH, warningLabel);
  84. }
  85.  
  86.  
  87. // This is where the problem is!
  88. public void actionPerformed(ActionEvent event) {
  89. if (!(Arrays.equals(password.getPassword(), confirmPassword.getPassword()))) {
  90. warningLabel.setText("Your passwords do not match! Please try again.");
  91. } else if (password.getPassword().length < 1) {
  92. warningLabel.setText("That password is not long enough! Please try again!");
  93. } else {
  94. try {
  95. FileWriter writer = new FileWriter(new File("nuserInfo.txt"));
  96. writer.write(username.getText() + "/" + password.toString());
  97. writer.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. }
  103. }
  104.  
  105. package passwordProgram;
  106.  
  107. import java.awt.BorderLayout;
  108. import java.awt.Color;
  109. import java.awt.GridBagConstraints;
  110. import java.awt.GridBagLayout;
  111. import java.awt.event.ActionEvent;
  112. import java.awt.event.ActionListener;
  113. import java.io.BufferedWriter;
  114. import java.io.FileWriter;
  115. import java.io.IOException;
  116. import java.util.Arrays;
  117.  
  118. import javax.swing.JButton;
  119. import javax.swing.JFrame;
  120. import javax.swing.JLabel;
  121. import javax.swing.JPanel;
  122. import javax.swing.JPasswordField;
  123. import javax.swing.JTextField;
  124. import javax.swing.SwingUtilities;
  125. import javax.swing.UIManager;
  126.  
  127. public class CreateAccount implements ActionListener {
  128.  
  129. JFrame frame;
  130. JPanel panel;
  131. JTextField username;
  132. JPasswordField password;
  133. JPasswordField confirmPassword;
  134. JLabel warningLabel;
  135.  
  136. public static void main(String[] args) {
  137. SwingUtilities.invokeLater(new Runnable() {
  138. @Override
  139. public void run() {
  140. try {
  141. UIManager.setLookAndFeel(UIManager
  142. .getSystemLookAndFeelClassName());
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. }
  146. CreateAccount window = new CreateAccount();
  147. window.go();
  148. }
  149. });
  150. }
  151.  
  152. public void go() {
  153. panel = new JPanel();
  154. panel.setLayout(new GridBagLayout());
  155. panel.setBackground(Color.ORANGE);
  156.  
  157. frame = new JFrame("Create a new account");
  158. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  159. frame.getContentPane().add(BorderLayout.CENTER, panel);
  160.  
  161. JLabel userLabel = new JLabel("Username:");
  162. JLabel passwordLabel = new JLabel("Password:");
  163. JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
  164. username = new JTextField(15);
  165. password = new JPasswordField(15);
  166. confirmPassword = new JPasswordField(15);
  167.  
  168. GridBagConstraints right = new GridBagConstraints();
  169. right.anchor = GridBagConstraints.WEST;
  170. GridBagConstraints left = new GridBagConstraints();
  171. left.anchor = GridBagConstraints.EAST;
  172.  
  173. right.weightx = (int) 2;
  174. right.fill = GridBagConstraints.REMAINDER;
  175. right.gridwidth = GridBagConstraints.REMAINDER;
  176. // actual GUI
  177.  
  178. panel.add(userLabel, left);
  179. panel.add(username, right);
  180. panel.add(passwordLabel, left);
  181. panel.add(password, right);
  182. panel.add(confirmPasswordLabel, left);
  183. panel.add(confirmPassword, right);
  184.  
  185. JButton createAccount = new JButton("Create this account");
  186. frame.getContentPane().add(BorderLayout.SOUTH, createAccount);
  187. createAccount.addActionListener(this);
  188.  
  189. warningLabel = new JLabel();
  190. frame.getContentPane().add(BorderLayout.NORTH, warningLabel);
  191.  
  192. frame.setSize(300, 250);
  193. frame.setVisible(true);
  194. }
  195.  
  196. // This is where the problem is!
  197. public void actionPerformed(ActionEvent event) {
  198. if (!(Arrays.equals(password.getPassword(),
  199. confirmPassword.getPassword()))) {
  200. warningLabel
  201. .setText("Your passwords do not match! Please try again.");
  202. } else if (password.getPassword().length < 1) {
  203. warningLabel
  204. .setText("That password is not long enough! Please try again!");
  205. } else {
  206. try {
  207. BufferedWriter writer = new BufferedWriter(new FileWriter(
  208. "nuserInfo.txt"));
  209. writer.write(username.getText() + "/"
  210. + new String(password.getPassword()));
  211. writer.close();
  212. } catch (IOException e) {
  213. e.printStackTrace();
  214. }
  215. }
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement