Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////
- // Author: Jonathan McRae
- // Date: 02-02-16
- //
- // Name: PassportFrame
- // Summary:
- //
- ////////////////////////////////////////////////////////////////////////////////////////////
- package Program2_Passport;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.event.*;
- import javax.swing.*;
- public class PassportFrame extends JFrame
- {
- // initializes GUI elements
- private JFrame frame;
- private PassportPanel panel;
- // creates GUI
- public PassportFrame()
- {
- // sets up the frame
- frame = new JFrame ("Passport Application");
- frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- // adds the content to the frame
- frame.getContentPane().add (panel);
- }
- // displays the frame
- public void display()
- {
- frame.pack();
- frame.setVisible(true);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- // Author: Jonathan McRae
- // Date: 02-02-16
- //
- // Name: PassportPanel
- // Summary:
- //
- ////////////////////////////////////////////////////////////////////////////////////////////
- package Program2_Passport;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.Random;
- public class PassportPanel extends JPanel
- {
- // initializes the random number
- private static Random randGen;
- // initializes the variable for the id number and (random) photo number
- private int idNumber;
- private int photoNumber;
- // initializes the GUI elements
- private JPanel panel;
- private JButton submitButton;
- private JButton newApplicant;
- private JLabel photo = new JLabel();
- private JLabel guiFirstName;
- private JLabel guiLastName;
- private JLabel guiIDNumber;
- private JTextField firstNameJTF = new JTextField(100);
- private JTextField lastNameJTF = new JTextField(100);
- public PassportPanel()
- {
- // sets up the random number generator
- randGen = new Random();
- // sets up the submit button
- submitButton = new JButton ("Submit");
- submitButton.addActionListener (new SubmitListener());
- // sets up the new applicant button
- newApplicant = new JButton ("New Applicant");
- newApplicant.addActionListener(new ApplicantListener());
- //sets up the labels
- photo.setIcon(new ImageIcon("/images/a1.jpg"));
- guiFirstName = new JLabel("First Name");
- guiLastName = new JLabel("Last Name");
- guiIDNumber = new JLabel("Identification Number");
- // sets up the rest of the text fields
- firstNameJTF.addActionListener(new NameListener());
- lastNameJTF.addActionListener(new NameListener());
- // sets up the panel
- panel = new JPanel();
- panel.setPreferredSize (new Dimension(800,800));
- panel.setBackground(Color.GRAY);
- panel.add(photo);
- panel.add(guiFirstName);
- panel.add(guiLastName);
- panel.add(guiIDNumber);
- panel.add(submitButton);
- }
- // listener for the submit button ('enters' the passport applicant)
- private class SubmitListener implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- // randomly generates a number to pick a random photo from the image library
- photoNumber = randGen.nextInt((8)+1);
- photo.setIcon(new ImageIcon("/images/a" + photoNumber +".jpeg"));
- // randomly generates the id number, converts to string, formats it, and sets the label
- idNumber = randGen.nextInt((999999999)+1);
- String idString = Integer.toString(idNumber);
- idString.format("%09");
- guiIDNumber.setText(idString);
- }
- }
- // listener for the new applicant button (prepares for new applicant)
- private class ApplicantListener implements ActionListener
- {
- // clears the text fields, id number, and photo to prepare for a new applicant entry
- public void actionPerformed(ActionEvent event)
- {
- photo.setText(""); guiIDNumber.setText(""); guiFirstName.setText(""); guiLastName.setText("");
- }
- }
- // listener for the name text fields
- private class NameListener implements ActionListener
- {
- // sets up the text field and sets the label to the input of the first name field
- public void actionPerformed(ActionEvent e)
- {
- String input = firstNameJTF.getText();
- guiFirstName.setText(input);
- }
- // sets up the text field and sets the label to the input of the last name field
- public void actionPerformed1(ActionEvent f)
- {
- String input = lastNameJTF.getText();
- guiLastName.setText(input);
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- // Author: Jonathan McRae
- // Date: 02-02-16
- //
- // Name: PassportUser
- // Summary:
- //
- ////////////////////////////////////////////////////////////////////////////////////////////
- package Program2_Passport;
- import java.text.DecimalFormat;
- import java.util.Random;
- public class PassportUser
- {
- public static void main (String[] args)
- {
- PassportFrame newPassportFrame = new PassportFrame();
- newPassportFrame.display();
- }
- }
- Exception in thread "main" java.lang.NullPointerException
- at java.awt.Container.addImpl(Unknown Source)
- at java.awt.Container.add(Unknown Source)
- at Program2_Passport.PassportFrame.<init>(PassportFrame.java:34)
- at Program2_Passport.PassportUser.main(PassportUser.java:18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement