/*
Chapter 8:
Program 14
Programmer:
Brad Shedd
Date:
April 15, 2004
Program Name:
W2Form.java
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public
class
W2Form
extends JFrame
implements
ActionListener
{
//Declare
output stream
DataOutputStream output;
//Construct
panel for each row
JPanel firstRow =
new
JPanel();
JPanel secondRow =
new
JPanel();
JPanel thirdRow =
new
JPanel();
JPanel fourthRow =
new
JPanel();
JPanel fifthRow =
new
JPanel();
JPanel sixthRow =
new
JPanel();
JPanel seventhRow =
new
JPanel();
JPanel eighthRow =
new
JPanel();
JPanel ninthRow =
new
JPanel();
//Construct
a panel for the fields and buttons
JPanel fieldPanel =
new
JPanel();
JPanel buttonPanel =
new
JPanel();
//Construct
labels and text boxes
JLabel controlNumberLabel =
new JLabel("a
Control Number :
");
JTextField controlNumber =
new
JTextField(15);
JLabel officialLabel =
new
JLabel("For Official Use Only :
");
JTextField official =
new
JTextField(15);
JLabel employerIDLabel =
new JLabel("b
Employer ID Number:
");
JTextField employerID =
new
JTextField(15);
JLabel wagesLabel =
new
JLabel("1 Wages, tips, etc.:
");
JTextField wages =
new
JTextField(7);
JLabel federalIncomeLabel =
new
JLabel("2 Federal Income Tax Withheld:
");
JTextField federalIncome =
new
JTextField(7);
JLabel employerNameAddressZipLabel =
new
JLabel("c Employer's Name, Address, Zip Code:
");
JTextField employerNameAddressZip =
new
JTextField(35);
JLabel ssWagesLabel =
new
JLabel("3 Social Security Wages:
");
JTextField ssWages =
new
JTextField(7);
JLabel ssWithheldLabel =
new
JLabel("4 Social Security Tax Withheld:
");
JTextField ssWithheld =
new
JTextField(7);
JLabel medicareWagesLabel =
new
JLabel("5 Medicare Wages, tips, etc.:
");
JTextField medicareWages =
new
JTextField(7);
JLabel medicareTaxLabel =
new
JLabel("6 Medicare Tax Withheld:
");
JTextField medicareTax =
new
JTextField(7);
JLabel ssTipsLabel =
new
JLabel("7 Social Security Tips:
");
JTextField ssTips =
new
JTextField(7);
JLabel allocatedTipsLabel =
new
JLabel("8 Allocated Tips:
");
JTextField allocatedTips =
new
JTextField(7);
JLabel employeeSocialSecurityNumLabel =
new
JLabel("d Employee's Social Security Number:");
JTextField
employeeSocialSecurityNum =
new
JTextField(10);
JLabel eicLabel =
new
JLabel("9 Advance EIC payment:");
JTextField eic=
new
JTextField(7);
JLabel careLabel =
new
JLabel("10 Dependent care benefits:");
JTextField care=
new
JTextField(7);
JLabel employeeNameLabel =
new
JLabel("e Employee's First, Initial, Last Name:");
JTextField employeeName =
new
JTextField(15);
JLabel employeeAddressLabel =
new
JLabel("f Employee's address and ZIP code:
");
JTextField employeeAddress =
new
JTextField(15);
JLabel nonqualLabel =
new
JLabel("11 Nonqualified plans:");
JTextField nonqual=
new
JTextField(7);
JLabel instructionsLabel =
new
JLabel("12a See Instructions for box 12:");
JTextField instructions=
new
JTextField(7);
JLabel twelveBLabel =
new
JLabel("twelveB:
");
JTextField twelveB=
new
JTextField(7);
JLabel twelveCLabel =
new
JLabel("twelveC:
");
JTextField twelveC=
new
JTextField(7);
JLabel twelveDLabel =
new
JLabel("D:
");
JTextField twelveD=
new
JTextField(7);
JLabel statutoryLabel =
new
JLabel("13 Statutory Employee:
");
JTextField statutory=
new
JTextField(10);
JLabel retirementLabel =
new
JLabel("Retirement Plan:");
JTextField retirement =
new
JTextField(7);
JLabel thirdPartyLabel =
new
JLabel("Third-party sick pay:");
JTextField thirdParty =
new
JTextField(7);
JLabel otherLabel =
new
JLabel("14 Other:");
JTextField other =
new
JTextField(7);
JLabel stateLabel =
new
JLabel("15 State:");
JTextField state =
new
JTextField(2);
JLabel employeersStateIDLabel =
new
JLabel("Employeer's State ID:");
JTextField employeersStateID =
new
JTextField(25);
JLabel stateWagesLabel =
new
JLabel("16 State Wages, tips, etc.:");
JTextField stateWages =
new
JTextField(7);
JLabel stateIncomeTaxLabel =
new
JLabel("17 State Income Tax:");
JTextField stateIncomeTax =
new
JTextField(7);
JLabel localWagesLabel =
new
JLabel("18 Local wages, tips, etc.:");
JTextField localWages =
new
JTextField(7);
JLabel localIncomeTaxLabel =
new
JLabel("19 Local Income Tax:");
JTextField localIncomeTax=
new
JTextField(7);
JLabel localityNameLabel =
new
JLabel("20 Locality Name:");
JTextField localityName=
new
JTextField(10);
//Construct
button
JButton submitButton =
new
JButton("Submit");
JButton exitButton =
new
JButton("Exit");
public
static
void
main(String[] args)
{
//set the
look and feel of the interface
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception
e)
{
JOptionPane.showMessageDialog(null,
"The
UIManager could not set the Look and Feel for this application.","Error",
JOptionPane.INFORMATION_MESSAGE);
}
W2Form f =
new
W2Form();
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.setSize(850,975);
f.setTitle("W-2
Tax Form");
f.setResizable(false);
f.setLocation(750,650);
f.setVisible(true);
}
public
W2Form()
{
Container c =
getContentPane();
c.setLayout((new
BorderLayout()));
fieldPanel.setLayout(new
GridLayout(8,1));
FlowLayout rowSetup =
new
FlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
fifthRow.setLayout(rowSetup);
sixthRow.setLayout(rowSetup);
seventhRow.setLayout(rowSetup);
eighthRow.setLayout(rowSetup);
ninthRow.setLayout(rowSetup);
buttonPanel.setLayout(new
FlowLayout(FlowLayout.CENTER));
//Add fields
to rows
firstRow.add(controlNumberLabel);
firstRow.add(controlNumber);
firstRow.add(employerIDLabel);
firstRow.add(employerID);
secondRow.add(officialLabel);
secondRow.add(official);
secondRow.add(wagesLabel);
secondRow.add(wages);
secondRow.add(federalIncomeLabel);
secondRow.add(federalIncome);
thirdRow.add(employerNameAddressZipLabel);
thirdRow.add(employerNameAddressZip);
thirdRow.add(employeeSocialSecurityNumLabel);
thirdRow.add(employeeSocialSecurityNum);
thirdRow.add(employeeNameLabel);
thirdRow.add(employeeName);
fourthRow.add(ssWagesLabel);
fourthRow.add(ssWages);
fourthRow.add(ssWithheldLabel);
fourthRow.add(ssWithheld);
fourthRow.add(medicareWagesLabel);
fourthRow.add(medicareWages);
fourthRow.add(medicareTaxLabel);
fourthRow.add(medicareTax);
fifthRow.add(employeeAddressLabel);
fifthRow.add(employeeAddress);
fifthRow.add(ssTipsLabel);
fifthRow.add(ssTips);
fifthRow.add(allocatedTipsLabel);
fifthRow.add(allocatedTips);
fifthRow.add(eicLabel);
fifthRow.add(eic);
fifthRow.add(careLabel);
fifthRow.add(care);
sixthRow.add(nonqualLabel);
sixthRow.add(nonqual);
sixthRow.add(instructionsLabel);
sixthRow.add(instructions);
sixthRow.add(twelveBLabel);
sixthRow.add(twelveB);
sixthRow.add(twelveCLabel);
sixthRow.add(twelveC);
sixthRow.add(twelveDLabel);
sixthRow.add(twelveD);
seventhRow.add(statutoryLabel);
seventhRow.add(statutory);
seventhRow.add(retirementLabel);
seventhRow.add(retirement);
seventhRow.add(thirdPartyLabel);
seventhRow.add(thirdParty);
seventhRow.add(otherLabel);
seventhRow.add(other);
eighthRow.add(stateLabel);
eighthRow.add(state);
eighthRow.add(employeersStateIDLabel);
eighthRow.add(employeersStateID);
eighthRow.add(stateWagesLabel);
eighthRow.add(stateWages);
eighthRow.add(stateIncomeTaxLabel);
eighthRow.add(stateIncomeTax);
ninthRow.add(localWagesLabel);
ninthRow.add(localWages);
ninthRow.add(localIncomeTaxLabel);
ninthRow.add(localIncomeTax);
ninthRow.add(localityNameLabel);
ninthRow.add(localityName);
//Add rows
to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
fieldPanel.add(fifthRow);
fieldPanel.add(sixthRow);
fieldPanel.add(seventhRow);
fieldPanel.add(eighthRow);
fieldPanel.add(ninthRow);
//Add button
to panel
buttonPanel.add(submitButton);
buttonPanel.add(exitButton);
//Add panels
to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel,
BorderLayout.SOUTH);
//Add
functionality to buttons
submitButton.addActionListener(this);
exitButton.addActionListener(this);
//Get the
current date and open the file
Date today =
new
Date();
SimpleDateFormat myFormat =
new
SimpleDateFormat("MMddyyyy");
String filename =
"payments"
+ myFormat.format(today);
try
{
output =
new
DataOutputStream(new
FileOutputStream("W2Form.dat"));
}
catch(IOException
io)
{
JOptionPane.showMessageDialog(null,
"The program
could not create a storage location. Please check the disk drive and then run
the program again."
,"Error",JOptionPane.INFORMATION_MESSAGE);
System.exit(1);
}
addWindowListener(
new
WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
int
answer = JOptionPane.showConfirmDialog(null,
"Are you
sure you want to exit and submit the file?",
"File Submission",
JOptionPane.YES_NO_OPTION);
if
(answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);
}
public
void actionPerformed(ActionEvent e)
{
String arg =
e.getActionCommand();
if(arg
==
"Submit")
{
try
{
output.writeUTF(controlNumber.getText());
output.writeUTF(official.getText());
output.writeUTF(employerID.getText());
output.writeUTF(wages.getText());
output.writeUTF(federalIncome.getText());
output.writeUTF(employerNameAddressZip.getText());
output.writeUTF(ssWages.getText());
output.writeUTF(ssWithheld.getText());
output.writeUTF(medicareWages.getText());
output.writeUTF(medicareTax.getText());
output.writeUTF(ssTips.getText());
output.writeUTF(allocatedTips.getText());
output.writeUTF(employeeSocialSecurityNum.getText());
output.writeUTF(eic.getText());
output.writeUTF(care.getText());
output.writeUTF(employeeName.getText());
output.writeUTF(nonqual.getText());
output.writeUTF(instructions.getText());
output.writeUTF(twelveB.getText());
output.writeUTF(twelveC.getText());
output.writeUTF(twelveD.getText());
output.writeUTF(statutory.getText());
output.writeUTF(retirement.getText());
output.writeUTF(thirdParty.getText());
output.writeUTF(other.getText());
output.writeUTF(employeeAddress.getText());
output.writeUTF(state.getText());
output.writeUTF(employeersStateID.getText());
output.writeUTF(stateWages.getText());
output.writeUTF(stateIncomeTax.getText());
output.writeUTF(localWages.getText());
output.writeUTF(localIncomeTax.getText());
output.writeUTF(localityName.getText());
JOptionPane.showMessageDialog(null,"The
course information has been saved.",
"Submission
Successful",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException
ex)
{
System.exit(1);
}
clearFields(
);
}
else
//code to execute if the user clicks Exit
{
try
{
output.close();
}
catch(IOException
c)
{
System.exit(1);
}
System.exit(0);
}
}
public
void clearFields()
{
//Clear
fields and reset the focus
controlNumber.setText("");
official.setText("");
employerID.setText("");
wages.setText("");
federalIncome.setText("");
employerNameAddressZip.setText("");
ssWages.setText("");
ssWithheld.setText("");
medicareWages.setText("");
medicareTax.setText("");
ssTips.setText("");
allocatedTips.setText("");
employeeSocialSecurityNum.setText("");
eic.setText("");
care.setText("");
employeeName.setText("");
nonqual.setText("");
instructions.setText("");
twelveB.setText("");
twelveC.setText("");
twelveD.setText("");
statutory.setText("");
retirement.setText("");
other.setText("");
employeeAddress.setText("");
stateWages.setText("");
stateIncomeTax.setText("");
localWages.setText("");
localIncomeTax.setText("");
localityName.setText("");
controlNumber.requestFocus();
}
}