I'm new to Java and trying to create a simple JFrame that has one button and four text fields. I'm trying to make my button be the calculation method. Mean when the user presses the "Calculate Pay" button it does the calculation of (HourlyRate * HoursWorked) - Deductions = TakeHomePay. I think I have all of my labels, panels and fields correct but I can't make my calculation work. Also, I'm not sure that I've added my ActionListener and ActionEvents correctly. Can any one please straighten me out. Thanks.
I am new to Java Programming and I need some help. I have to make a simple JFrame, have four textfields, and one button. I want my button to calculate a simple math equation.
My math: double TakeHomePay = (rate * hrsworked) - deductions
I used an example program from the java.com site that was kind of similar in the way of needing four text fields a frame. The math and calculation are not the same. (Which I need help with.) I have attached what I have so far. Can anyone help me? I need to make sure my JButton command is correct and I need help with the math formula and where to put it into the syntax at. Thanks.
Oh, it can't be used with Netbean..which the example that I used is from. Please check my ActionListener and ActionEvent.. do I have those set right?
Thanks,package components; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.*; /** * PayCalculator.java requires no other files. * * It implements a simple program to calculate what the pay rate of an employee would * would have after pay rate times hours worked minus deductions. */ public class PayCalculator extends JPanel implements ActionListener { //Values for the fields private double HourlyRate = (); private double HoursWorked = (); private int Deductions = (); //Labels to identify the fields private JLabel HourlyRateLabel; private JLabel HoursWorkedLabel; private JLabel DeductionsLabel; private JLabel HomePayLabel; //Strings for the labels private static String HourlyRateString = "Enter Hourly Rate: "; private static String HoursWorkedString = "Hours Worked "; private static String DeductionsString = "Deductions "; private static String HomePayString = "Take Home Pay: "; //Fields for data entry private JPayCalculator HourlyRateField; private JPayCalculator HoursWorkedField; private JPayCalculator DeductionsField; private JPayCalculator HomePayField; //Formats to format and parse numbers private NumberFormat HourlyRateFormat; private NumberFormat HoursWorkedFormat; private NumberFormat DeductionsFormat; private NumberFormat HomePayFormat; public PayCalculator() { super(new BorderLayout()); setUpFormats(); double pay = computePay(hourly rate, hours worked, deductions); //Create the labels. HourlyRateLabel = new JLabel(HourlyRateString); HoursWorkedLabel = new JLabel(HoursWorkedString); DeductionsLabel = new JLabel(DeductionsString); HomePayLabel = new JLabel(HomePayString); JLabel ButtonLabel = new JLabel("Calculate Take Home Pay"); //Create the text fields and set them up. HourlyRateField = new JPayCalculator(HourlyRateFormat); HourlyRate.setValue(new Double(HourlyRate)); HourlyRate.setColumns(10); HourlyRate.addPropertyChangeListener("value", this); HoursWorkedField = new JPayCalculator(percentFormat); HoursWorked.setValue(new Double(rate)); HoursWorked.setColumns(10); HoursWorked.addPropertyChangeListener("value", this); DeductionsField = new JPayCalculator(); DeductionsField.setValue(new Integer(numPeriods)); DeductionsField.setColumns(10); DeductionsField.addPropertyChangeListener("value", this); HomePayField = new JPayCalculator(paymentFormat); HomePay.setValue(new Double(payment)); HomePay.setColumns(10); HomePay.setEditable(false); HomePay.setForeground(Color.green); //Tell accessibility tools about label/textfield pairs. HourlyRateLabel.setLabelFor(HourlyRateField); HoursWorkedLabel.setLabelFor(HoursWorkedField); DeductionsLabel.setLabelFor(DeductionsField); HomePayLabel.setLabelFor(HomePayField); //Lay out the labels in a panel. JPanel labelPane = new JPanel(new GridLayout(0,1)); labelpane.add(HourlyRateLabel); labelpane.add(HoursWorkedLabel); labelpane.add(DeductionsLabel); labelpane.add(HomePayLabel); //Layout the text fields in a panel. JPanel fieldPane = new JPanel(new GridLayout(0,1)); fieldPane.add(HourlyRateField); fieldPane.add(HoursWorkedField); fieldPane.add(DeductionsField); fieldPane.add(HomePayField); //Put the panels in this panel, labels on left, //text fields on right. setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); add(labelPane, BorderLayout.CENTER); add(fieldPane, BorderLayout.LINE_END); } /** Called when a field's "value" property changes. */ public void double payment = computePayment(HourlyRate, HoursWorked, Deductions); payfield.setValue(new Double(pay)); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Pay Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add contents to the window. frame.add(new PayCalculator()); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { //Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI(); } }); } //Compute payment amount based on hourly rate times hours worked minus deductions equals take home pay. double computePayment(double HourlyRate, double HoursWorked, int Deductions) { double answer; HomePay = (); do ((HourlyRate * HoursWorked) - Deductions); } answer = ((HourlyRate * HoursWorked) - Deductions); return answer; } //Create and set up number formats. These objects also //parse numbers input by user. private void setUpFormats() { amountFormat = NumberFormat.getNumberInstance(); percentFormat = NumberFormat.getNumberInstance(); percentFormat.setMinimumFractionDigits(3); payFormat = NumberFormat.getCurrencyInstance(); } }