Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple JFrame : Help with what goes in it

  1. #1
    Junior Member IBWebN's Avatar
    Join Date
    Nov 2011
    Denver, Colorado
    Thanked 0 Times in 0 Posts

    Default Simple JFrame : Help with what goes in it

    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?

    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());
            double pay = computePay(hourly rate,
                                            hours worked,
            //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.addPropertyChangeListener("value", this);
            HoursWorkedField = new JPayCalculator(percentFormat);
            HoursWorked.setValue(new Double(rate));
            HoursWorked.addPropertyChangeListener("value", this);
            DeductionsField = new JPayCalculator();
            DeductionsField.setValue(new Integer(numPeriods));
            DeductionsField.addPropertyChangeListener("value", this);
            HomePayField = new JPayCalculator(paymentFormat);
            HomePay.setValue(new Double(payment));
            //Tell accessibility tools about label/textfield pairs.
            //Lay out the labels in a panel.
            JPanel labelPane = new JPanel(new GridLayout(0,1));
            //Layout the text fields in a panel.
            JPanel fieldPane = new JPanel(new GridLayout(0,1));
            //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");
            //Add contents to the window.
            frame.add(new PayCalculator());
            //Display the window.
        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);
        //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();
            payFormat = NumberFormat.getCurrencyInstance();
    Last edited by helloworld922; January 16th, 2012 at 09:42 PM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Simple JFrame : Help with what goes in it

    In the future please surround your code with code tags. This means adding [code=java] before your code, and [/code] after your code.

    I've added them for you this time.

    I don't see anywhere in your code where you added an ActionListener to your button. I would suggest reading How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) for how to do this.

Similar Threads

  1. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  2. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  3. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  4. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM