private JTextField fnameTF, lnameTF, hoursTF, hrateTF, idTF, weeknumTF, netPayTF;
private JLabel fnameL, lnameL, hoursL, hrateL, idL, weeknumL, netPayL;
//right column of GUI component for entering information in the fields
private JButton calculateB, resetB, addB, exitB; //Buttons listed on GUI component
private CalculateButtonHandler cbHandler;
private ResetButtonHandler rbHandler;
private AddButtonHandler abHandler;
private ExitButtonHandler ebHandler;
//methods objects for submit buttons on GUI
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
//height and width of GUI component}
public PayrollCalculator()
{
//Create the seven labels, right justified
fnameL = new JLabel("Enter First Name: ",SwingConstants.RIGHT);
lnameL = new JLabel("Enter Last Name: ",SwingConstants.RIGHT);
hoursL = new JLabel("Enter Hours Worked: ", SwingConstants.RIGHT);
hrateL = new JLabel("Enter Hourly Rate: ", SwingConstants.RIGHT);
idL = new JLabel("Enter Employee ID: ", SwingConstants.RIGHT);
weeknumL = new JLabel("Enter Week Number: ", SwingConstants.RIGHT);
netPayL = new JLabel("Calculated Net Pay: ", SwingConstants.RIGHT);
//Create the seven text fields, ten characters in length
fnameTF = new JTextField(20);
lnameTF = new JTextField(20);
hoursTF = new JTextField(20);
hrateTF = new JTextField(20);
idTF = new JTextField(20);
weeknumTF = new JTextField(20);
netPayTF = new JTextField(20);
//Create Calculate Button to determine and show calculated net pay
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Reset Button to clear text in field and re-enter information
resetB = new JButton("Clear");
rbHandler = new ResetButtonHandler();
resetB.addActionListener(rbHandler);
//Create Add to File Button which will print all informatio to a file
addB = new JButton("Add to File");
abHandler = new AddButtonHandler();
addB.addActionListener(abHandler);
//Create Exit Button closes the GUI screen when complete
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Set the title of the window
setTitle("Calculate a Pay Check");
//Get the container
Container pane = getContentPane();
//Set the layout of rows and columns
pane.setLayout(new GridLayout(9, 2));
//Place the components in the pane
pane.add(fnameL);
pane.add(fnameTF);
pane.add(lnameL);
pane.add(lnameTF);
pane.add(hoursL);
pane.add(hoursTF);
pane.add(hrateL);
pane.add(hrateTF);
pane.add(idL);
pane.add(idTF);
pane.add(weeknumL);
pane.add(weeknumTF);
pane.add(netPayL);
pane.add(netPayTF);
pane.add(calculateB);
pane.add(addB);
pane.add(resetB);
pane.add(exitB);
//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void setTitle(String calculate_a_Pay_Check) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private Container getContentPane() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setSize(int WIDTH, int HEIGHT) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private void setVisible(boolean b) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double hours, rate, netPay;
//calculate pay and display in caluculated net pay
hours = Double.parseDouble(hoursTF.getText());
rate = Double.parseDouble(hrateTF.getText());
netPay = hours * rate;
netPayTF.setText("" + netPay);
}
}
private class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//resets JTextFields
fnameTF.setText("");
lnameTF.setText("");
hoursTF.setText("");
hrateTF.setText("");
idTF.setText("");
weeknumTF.setText("");
netPayTF.setText("");
}
}
//Add to File code
private class AddButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Stream to write file
FileOutputStream fout;
try
{
// Open an output stream
fout = new FileOutputStream ("myfile.txt");
// Print a line of text
new PrintStream(fout).println ("hello world!" + fnameTF.getText());
// Close the output stream
fout.close();
}
catch (IOException f)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}
private class ExitButtonHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
{
PayrollCalculator rectObject = new PayrollCalculator();
}
}