I am working on the payroll calculation program below and would like some help with the JTextField area. I would like the program to respond with a GUI window letting the user know that the JTextField is blank and must enter First name, last name, and so on. Any help would be greatly appreciated. Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PayrollProgram extends JFrame
{
private JLabel fnameL, lnameL, hoursL, hrateL, idL, weeknumL, numdepL;
private JTextField fnameTF, lnameTF, hoursTF, hrateTF, idTF, weeknumTF, numdepTF;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
public PayrollProgram()
{
//Create the four labels
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);
numdepL = new JLabel("Enter Number of Dependants: ", SwingConstants.RIGHT);
//Create the four text fields
fnameTF = new JTextField(10);
lnameTF = new JTextField(10);
hoursTF = new JTextField(10);
hrateTF = new JTextField(10);
idTF = new JTextField(10);
weeknumTF = new JTextField(10);
numdepTF = new JTextField(10);
//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Exit Button
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
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(numdepL);
pane.add(numdepTF);
pane.add(calculateB);
pane.add(exitB);
//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double width, length, area, perimeter;
/*length = Double.parseDouble(lengthTF.getText());
width = Double.parseDouble(widthTF.getText());
area = length * width;
perimeter = 2 * (length + width);
areaTF.setText("" + area);
perimeterTF.setText("" + perimeter);*/
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
PayrollProgram rectObject = new PayrollProgram();
}
}