Advertisement
Guest User

Passport Code

a guest
Feb 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////
  2. // Author: Jonathan McRae
  3. // Date: 02-02-16
  4. //
  5. // Name: PassportFrame
  6. // Summary:
  7. //
  8. ////////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. package Program2_Passport;
  11.  
  12. import java.awt.Component;
  13. import java.awt.Dimension;
  14. import java.awt.Color;
  15. import java.awt.event.*;
  16. import javax.swing.*;
  17.  
  18. public class PassportFrame extends JFrame
  19. {
  20. // initializes GUI elements
  21. private JFrame frame;
  22. private PassportPanel panel;
  23.  
  24. // creates GUI
  25. public PassportFrame()
  26. {
  27. // sets up the frame
  28. frame = new JFrame ("Passport Application");
  29. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  30.  
  31. // adds the content to the frame
  32. frame.getContentPane().add (panel);
  33. }
  34.  
  35.  
  36. // displays the frame
  37. public void display()
  38. {
  39. frame.pack();
  40. frame.setVisible(true);
  41. }
  42. }
  43.  
  44.  
  45. ////////////////////////////////////////////////////////////////////////////////////////////
  46. // Author: Jonathan McRae
  47. // Date: 02-02-16
  48. //
  49. // Name: PassportPanel
  50. // Summary:
  51. //
  52. ////////////////////////////////////////////////////////////////////////////////////////////
  53.  
  54. package Program2_Passport;
  55.  
  56. import java.awt.Dimension;
  57. import java.awt.Color;
  58. import java.awt.event.*;
  59.  
  60. import javax.swing.*;
  61.  
  62. import java.util.Random;
  63.  
  64. public class PassportPanel extends JPanel
  65. {
  66. // initializes the random number
  67. private static Random randGen;
  68.  
  69. // initializes the variable for the id number and (random) photo number
  70. private int idNumber;
  71. private int photoNumber;
  72.  
  73. // initializes the GUI elements
  74. private JPanel panel;
  75. private JButton submitButton;
  76. private JButton newApplicant;
  77. private JLabel photo = new JLabel();
  78. private JLabel guiFirstName;
  79. private JLabel guiLastName;
  80. private JLabel guiIDNumber;
  81. private JTextField firstNameJTF = new JTextField(100);
  82. private JTextField lastNameJTF = new JTextField(100);
  83.  
  84. public PassportPanel()
  85. {
  86. // sets up the random number generator
  87. randGen = new Random();
  88.  
  89. // sets up the submit button
  90. submitButton = new JButton ("Submit");
  91. submitButton.addActionListener (new SubmitListener());
  92.  
  93. // sets up the new applicant button
  94. newApplicant = new JButton ("New Applicant");
  95. newApplicant.addActionListener(new ApplicantListener());
  96.  
  97. //sets up the labels
  98. photo.setIcon(new ImageIcon("/images/a1.jpg"));
  99. guiFirstName = new JLabel("First Name");
  100. guiLastName = new JLabel("Last Name");
  101. guiIDNumber = new JLabel("Identification Number");
  102.  
  103. // sets up the rest of the text fields
  104. firstNameJTF.addActionListener(new NameListener());
  105. lastNameJTF.addActionListener(new NameListener());
  106.  
  107. // sets up the panel
  108. panel = new JPanel();
  109. panel.setPreferredSize (new Dimension(800,800));
  110. panel.setBackground(Color.GRAY);
  111. panel.add(photo);
  112. panel.add(guiFirstName);
  113. panel.add(guiLastName);
  114. panel.add(guiIDNumber);
  115. panel.add(submitButton);
  116. }
  117.  
  118. // listener for the submit button ('enters' the passport applicant)
  119. private class SubmitListener implements ActionListener
  120. {
  121. public void actionPerformed(ActionEvent event)
  122. {
  123. // randomly generates a number to pick a random photo from the image library
  124. photoNumber = randGen.nextInt((8)+1);
  125. photo.setIcon(new ImageIcon("/images/a" + photoNumber +".jpeg"));
  126.  
  127. // randomly generates the id number, converts to string, formats it, and sets the label
  128. idNumber = randGen.nextInt((999999999)+1);
  129. String idString = Integer.toString(idNumber);
  130. idString.format("%09");
  131. guiIDNumber.setText(idString);
  132. }
  133. }
  134.  
  135. // listener for the new applicant button (prepares for new applicant)
  136. private class ApplicantListener implements ActionListener
  137. {
  138. // clears the text fields, id number, and photo to prepare for a new applicant entry
  139. public void actionPerformed(ActionEvent event)
  140. {
  141. photo.setText(""); guiIDNumber.setText(""); guiFirstName.setText(""); guiLastName.setText("");
  142. }
  143. }
  144.  
  145. // listener for the name text fields
  146. private class NameListener implements ActionListener
  147. {
  148. // sets up the text field and sets the label to the input of the first name field
  149. public void actionPerformed(ActionEvent e)
  150. {
  151. String input = firstNameJTF.getText();
  152. guiFirstName.setText(input);
  153. }
  154.  
  155. // sets up the text field and sets the label to the input of the last name field
  156. public void actionPerformed1(ActionEvent f)
  157. {
  158. String input = lastNameJTF.getText();
  159. guiLastName.setText(input);
  160. }
  161. }
  162. }
  163.  
  164. ////////////////////////////////////////////////////////////////////////////////////////////
  165. // Author: Jonathan McRae
  166. // Date: 02-02-16
  167. //
  168. // Name: PassportUser
  169. // Summary:
  170. //
  171. ////////////////////////////////////////////////////////////////////////////////////////////
  172.  
  173. package Program2_Passport;
  174. import java.text.DecimalFormat;
  175. import java.util.Random;
  176.  
  177. public class PassportUser
  178. {
  179. public static void main (String[] args)
  180. {
  181. PassportFrame newPassportFrame = new PassportFrame();
  182. newPassportFrame.display();
  183. }
  184. }
  185.  
  186.  
  187. Exception in thread "main" java.lang.NullPointerException
  188. at java.awt.Container.addImpl(Unknown Source)
  189. at java.awt.Container.add(Unknown Source)
  190. at Program2_Passport.PassportFrame.<init>(PassportFrame.java:34)
  191. at Program2_Passport.PassportUser.main(PassportUser.java:18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement