/** * This application calculates the weekly pay for an employee given * its pay rate and number of hours worked that week. The rule for * calculating the pay is as follows: If the number of hours worked is * no more than 40, pay should be the rate times the number of hours worked. * If it's over 40 hours, overtime pay should be 1.5 the rate times the * overtime hours. * * @author Hongyan Wang */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.*; public class WageCalculatorGUI { // // main function // public static void main (String[] args) { // Create the panel that holds everything WageCalculatorPane cal = new WageCalculatorPane (); // Create the window that holds the panel JFrame win = new JFrame (); win.setSize (360, 180); win.setResizable (false); win.setLocation (300, 200); win.setTitle ("Weekly Pay Calculator"); win.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); win.setContentPane (cal); // stick panel to the window win.setVisible (true); } } class WageCalculatorPane extends JPanel implements ActionListener { // // private data members // private JLabel lbRate, lbHrs, lbWage; private JTextField tfRate, tfHrs, tfWage; private JButton btCal; public WageCalculatorPane () { initComponents (); } public void actionPerformed (ActionEvent e) { Object src = e.getSource (); if (src == tfRate) { // just set focus to textfield for hours worked tfHrs.requestFocus (); } else if (src == tfHrs || src == btCal) { btCal.requestFocus (); // calculate and display the result calPay (); } } // // private methods // private void initComponents () { GridBagConstraints c; // Create the components lbRate = new JLabel("Pay Rate", JLabel.RIGHT); tfRate = new JTextField(10); tfRate.addActionListener (this); lbHrs = new JLabel ("Hours Worked", JLabel.RIGHT); tfHrs = new JTextField (10); tfHrs.addActionListener (this); btCal = new JButton("Calculate"); btCal.addActionListener (this); lbWage = new JLabel("Weekly Pay", JLabel.RIGHT); tfWage = new JTextField(10); tfWage.setEditable (false); // Add components to frame setLayout(new GridBagLayout()); // Add Pay Rate label c = new GridBagConstraints(); c.gridx = 1; c.gridy = 0; c.gridwidth = 2; c.fill = GridBagConstraints.BOTH; add(lbRate, c); // Add text field for pay rate c = new GridBagConstraints(); c.gridx = 3; c.gridy = 0; c.gridwidth = 3; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 21, 5, 0); add(tfRate, c); // Add label for hours worked c = new GridBagConstraints(); c.gridx = 1; c.gridy = 1; c.gridwidth = 2; c.fill = GridBagConstraints.BOTH; c.insets = new Insets(5, 0, 5, 0); add(lbHrs, c); // Add textfield for hours worked c = new GridBagConstraints(); c.gridx = 3; c.gridy = 1; c.gridwidth = 3; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 21, 5, 0); add(tfHrs, c); // Add button to calculate c = new GridBagConstraints(); c.gridx = 6; c.gridy = 2; c.gridwidth = GridBagConstraints.REMAINDER; c.insets = new Insets (5, 21, 5, 0); add(btCal, c); // Add label for wage c = new GridBagConstraints (); c.gridx = 1; c.gridy = 2; c.gridwidth = 2; c.insets = new Insets (5, 0, 5, 0); add (lbWage, c); // Add text field c = new GridBagConstraints (); c.gridx = 3; c.gridy = 2; c.gridwidth = 3; c.insets = new Insets (5, 21, 5, 0); add (tfWage, c); } private void calPay () { double rate, hrs, pay; try { rate = Double.parseDouble (tfRate.getText ()); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog (this, "Pay rate is not a properly formatted number"); tfRate.requestFocus (); return; } try { hrs = Double.parseDouble (tfHrs.getText ()); } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog (this, "Pay rate is not a properly formatted number"); tfHrs.requestFocus (); return; } if (rate < 0.0) { JOptionPane.showMessageDialog (this, "Pay rate can't be negative"); tfRate.requestFocus (); return; } else if (hrs < 0.0) { JOptionPane.showMessageDialog (this, "Hours worked can't be negative"); tfHrs.requestFocus (); return; } if (hrs > 40.0) pay = rate * 40.0 + rate * (hrs - 40.0) * 1.5; else pay = rate * hrs; DecimalFormat d2 = new DecimalFormat ("0.00"); tfWage.setText (d2.format (pay)); } }