rendered paste bodyimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class PersonGUI implements ActionListener
{
private ArrayList<Person> peopleDatabase;
// Textfields for basic information
private JTextField fName;
private JTextField lName;
private JTextField address;
private JTextField city;
private JTextField zip;
private JComboBox state;
private JTextArea message;
private JButton enter;
private JButton count;
private JMenuItem exitItem;
private JMenuItem aboutItem;
public PersonGUI()
{
peopleDatabase = new ArrayList<Person>();
//initialize frame
JFrame personFrame = new JFrame("Person Database Entry");
personFrame.setSize(600,200);
personFrame.setLocation(100,100);
personFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
personFrame.setResizable(false);
JPanel top = new JPanel();
top.add(new JLabel("First Name: "));
fName = new JTextField(10);
top.add(fName);
top.add(new JLabel("Last Name: "));
lName = new JTextField(15);
top.add(lName);
personFrame.add(top,BorderLayout.NORTH);
JPanel mid = new JPanel();
mid.setLayout(new GridLayout(0,1));
JPanel addrPanel = new JPanel();
addrPanel.add(new JLabel("Address: "));
address = new JTextField(40);
addrPanel.add(address);
mid.add(addrPanel);
JPanel cszPanel = new JPanel();
cszPanel.add(new JLabel("City: "));
city = new JTextField(15);
cszPanel.add(city);
cszPanel.add(new JLabel("State: "));
state = new JComboBox();
state.addItem("NY");
state.addItem("OH");
state.addItem("PA");
state.addItem("RI");
state.addItem("MA");
state.setSelectedItem(null);
cszPanel.add(state);
cszPanel.add(new JLabel("Zip: "));
zip = new JTextField(5);
cszPanel.add(zip);
mid.add(cszPanel);
personFrame.add(mid,BorderLayout.CENTER);
JPanel side = new JPanel();
side.setLayout(new GridLayout(0,1));
JPanel b1 = new JPanel();
enter = new JButton("Enter");
b1.add(enter);
enter.addActionListener(this);
side.add(b1);
JPanel b2 = new JPanel();
count = new JButton("Count");
b2.add(count);
count.addActionListener(this);
side.add(b1);
side.add(b2);
personFrame.add(side,BorderLayout.WEST);
JPanel bottom = new JPanel();
message = new JTextArea(2,30);
message.setLineWrap(true);
message.setWrapStyleWord(true);
message.setEditable(false);
JScrollPane jsp = new JScrollPane(message);
bottom.add(jsp);
personFrame.add(bottom,BorderLayout.SOUTH);
JMenuBar jmb = new JMenuBar();
personFrame.setJMenuBar(jmb);
JMenu mHW = new JMenu("Homework");
mHW.setMnemonic('H');
jmb.add(mHW);
aboutItem = new JMenuItem("About");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(this);
mHW.add(aboutItem);
exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(this);
mHW.add(exitItem);
//display frame
personFrame.pack();
personFrame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == exitItem)
{
System.exit(0);
}
else if (ae.getSource() == aboutItem)
{
message.setText("Your Name, 221, Homework 6: allow entry of "+
"information on a person and save in a list and find out how many you have entered.");
}
else if (ae.getSource() == count)
{
message.setText("The number of people created and saved is: "+peopleDatabase.size());
resetFields();
}
else if (ae.getSource() == enter)
{
try
{
Person pers = new Person(fName.getText(),lName.getText(),address.getText(),
city.getText(),state.getSelectedItem().toString(),zip.getText());
peopleDatabase.add(pers);
message.setText(fName.getText()+" "+lName.getText()+" created and saved");
resetFields();
}
catch (BadValueException bve)
{
message.setText("Problem saving this person, please correct: "+
bve.getMessage());
fName.requestFocus();
}
catch (NullPointerException npe)
{
message.setText("Problem saving this person, please enter the requested information.");
fName.requestFocus();
}
} //enter
} //action performed
public void resetFields()
{
fName.setText("");
fName.requestFocus();
lName.setText("");
address.setText("");
city.setText("");
state.setSelectedItem(null);
zip.setText("");
}
public static void main(String[] args)
{
PersonGUI gui = new PersonGUI();
}
} //end class