Guest User

Untitled

a guest
Dec 5th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. package net.codejava.swing.jpanel;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.util.ArrayList;
  14.  
  15. import javax.swing.BorderFactory;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.JPasswordField;
  21. import javax.swing.JTextField;
  22. import javax.swing.SwingUtilities;
  23. import javax.swing.UIManager;
  24.  
  25. public class SignUp extends JFrame {
  26.  
  27. private JLabel usernameLabel = new JLabel("Username: ");
  28. private JLabel passwordLabel = new JLabel("Password: ");
  29. private JLabel firstNameLabel = new JLabel("First Name: ");
  30. private JLabel lastNameLabel = new JLabel("Last Name: ");
  31. private JLabel numberLabel = new JLabel("Number: ");
  32. private JLabel birthdayLabel = new JLabel("Birthday: ");
  33. private JLabel cityLabel = new JLabel("City: ");
  34. private JTextField textUsername = new JTextField(30);
  35. private JPasswordField passwordField = new JPasswordField(30);
  36. private JTextField firstNameField = new JTextField(30);
  37. private JTextField lastNameField = new JTextField(30);
  38. private JTextField numberField = new JTextField(30);
  39. private JTextField birthdayField = new JTextField(30);
  40. private JTextField cityField = new JTextField(30);
  41.  
  42. private JButton signUpButton = new JButton("Sign Up");
  43.  
  44. public SignUp(ArrayList<User> userInfo)
  45. {
  46. super("Sign Up");
  47.  
  48. // create a new panel with GridBagLayout manager
  49. JPanel newPanel = new JPanel(new GridBagLayout());
  50.  
  51. GridBagConstraints constraints = new GridBagConstraints();
  52. constraints.anchor = GridBagConstraints.WEST;
  53. constraints.insets = new Insets(10, 10, 10, 10);
  54.  
  55. // add components to the panel
  56. constraints.gridx = 0;
  57. constraints.gridy = 0;
  58. newPanel.add(usernameLabel, constraints);
  59.  
  60. constraints.gridx = 1;
  61. newPanel.add(textUsername, constraints);
  62.  
  63. constraints.gridx = 0;
  64. constraints.gridy = 1;
  65. newPanel.add(passwordLabel, constraints);
  66.  
  67. constraints.gridx = 1;
  68. newPanel.add(passwordField, constraints);
  69.  
  70. constraints.gridx = 0;
  71. constraints.gridy = 2;
  72. newPanel.add(firstNameLabel, constraints);
  73.  
  74. constraints.gridx = 1;
  75. newPanel.add(firstNameField, constraints);
  76.  
  77. constraints.gridx = 0;
  78. constraints.gridy = 3;
  79. newPanel.add(lastNameLabel, constraints);
  80.  
  81. constraints.gridx = 1;
  82. newPanel.add(lastNameField, constraints);
  83.  
  84. constraints.gridx = 0;
  85. constraints.gridy = 4;
  86. newPanel.add(numberLabel, constraints);
  87.  
  88. constraints.gridx = 1;
  89. newPanel.add(numberField, constraints);
  90.  
  91. constraints.gridx = 0;
  92. constraints.gridy = 5;
  93. newPanel.add(birthdayLabel, constraints);
  94.  
  95. constraints.gridx = 1;
  96. newPanel.add(birthdayField, constraints);
  97.  
  98. constraints.gridx = 0;
  99. constraints.gridy = 6;
  100. newPanel.add(cityLabel, constraints);
  101.  
  102. constraints.gridx = 1;
  103. newPanel.add(cityField, constraints);
  104.  
  105. constraints.gridx = 0;
  106. constraints.gridy = 7;
  107. constraints.gridwidth = 6;
  108. constraints.anchor = GridBagConstraints.CENTER;
  109. newPanel.add(signUpButton, constraints);
  110.  
  111. // set border for the panel
  112. newPanel.setBorder(BorderFactory.createTitledBorder(
  113. BorderFactory.createEtchedBorder(), "Sign Up Panel"));
  114.  
  115. // add the panel to this frame
  116. add(newPanel);
  117.  
  118. pack();
  119. setLocationRelativeTo(null);
  120.  
  121. signUpButton.addActionListener(new ActionListener()
  122. {
  123. public void actionPerformed(ActionEvent e)
  124. {
  125. if(!signUpButton.getModel().isPressed())
  126. {
  127. User user = new User(textUsername.getText(), firstNameField.getText(),
  128. lastNameField.getText(), numberField.getText(),
  129. birthdayField.getText(), cityField.getText(), passwordField.getPassword());
  130.  
  131. userInfo.add(user);
  132.  
  133. try
  134. {
  135. FileOutputStream fos= new FileOutputStream("users.txt", false);
  136. ObjectOutputStream oos= new ObjectOutputStream(fos);
  137. oos.writeObject(userInfo);
  138. oos.close();
  139. fos.close();
  140. }
  141. catch(IOException ioe)
  142. {
  143. ioe.printStackTrace();
  144. }
  145.  
  146. try
  147. {
  148. FileInputStream fileIn = new FileInputStream("users.txt");
  149. ObjectInputStream in = new ObjectInputStream(fileIn);
  150. ArrayList<User> userList = (ArrayList<User>) in.readObject();
  151. in.close();
  152. fileIn.close();
  153. }
  154. catch(IOException i)
  155. {
  156. i.printStackTrace();
  157. return;
  158. }
  159.  
  160. catch(ClassNotFoundException c)
  161. {
  162. System.out.println("User class not found");
  163. c.printStackTrace();
  164. return;
  165. }
  166.  
  167. System.out.println("Deserialized User...");
  168. System.out.println("Name: " + userInfo.get(0).getUsername());
  169. System.out.println("Name: " + userInfo.get(0).getFirstName());
  170. System.out.println("Name: " + userInfo.get(0).getLastName());
  171. System.out.println("Name: " + userInfo.get(0).getNumberID());
  172. System.out.println("Name: " + userInfo.get(0).getBirthday());
  173. System.out.println("Name: " + userInfo.get(0).getCity());
  174. System.out.println("Name: " + userInfo.get(0).getNumber());
  175. }
  176. }
  177. });
  178.  
  179. addWindowListener(new java.awt.event.WindowAdapter()
  180. {
  181. public void windowClosing(java.awt.event.WindowEvent e)
  182. {
  183. new MainMenu(); // Main Form to show after the Login Form..
  184. }
  185. });
  186. }
  187.  
  188. public static void main(String[] args)
  189. {
  190. try
  191. {
  192. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  193. }
  194. catch (Exception ex)
  195. {
  196. ex.printStackTrace();
  197. }
  198.  
  199. SwingUtilities.invokeLater(new Runnable()
  200. {
  201. @Override
  202. public void run()
  203. {
  204. new SignUp(MainMenu.userList).setVisible(true);
  205. }
  206. });
  207. }
  208. }
Add Comment
Please, Sign In to add comment