Advertisement
Guest User

Untitled

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