Hi guys, i am writing a GUI program that will take a tutor minutes and earings and create an average earnings. My problem is instead of it using what we type in the fields E.g 200 mins and 50 dollars earnt and displaying the average. It shows the default fields. Please see attached code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Tutorhelp extends JFrame 
{
    private JPanel panel;
    private JLabel TutorTimeLabel;
    private JTextField timeField;
    private JLabel TutorWageLabel;
    private JTextField wageField;
    private JTextArea textArea;
    private JButton enterButton;
    private JButton runReportsButton;
    private JButton quitButton;
    final int WINDOW_WIDTH = 500;
    final int WINDOW_HEIGHT = 600;
    double [] tutorMin = new double [2];
    double [] tutorWage = new double [2];


    public Tutorhelp()
    {
        setTitle("Tutor Earnings");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);

    }

    private void buildPanel()
    {
        TutorTimeLabel = new JLabel("Tutor Time");
        timeField = new JTextField(10);
        TutorWageLabel = new JLabel ("Tutor Earnings");
        wageField = new JTextField(10);
        textArea = new JTextArea(25,50);
        enterButton = new JButton("Enter");
        enterButton.addActionListener(new EnterButtonListener());
        runReportsButton = new JButton("Run Reports");
        runReportsButton.addActionListener(new runReportsButtonListener());
        quitButton = new JButton("Quit");
        quitButton.addActionListener(new quitButtonListener());
        panel = new JPanel();
        panel.add(TutorTimeLabel);
        panel.add(timeField);
        panel.add(TutorWageLabel);
        panel.add(wageField);
        panel.add(textArea);
        panel.add(enterButton);
        panel.add(runReportsButton);
        panel.add(quitButton);    
    }

    private class EnterButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

        int count = 0;
        double num1 = 0;
        double num2 = 0;
        double m = Double.parseDouble(timeField.getText());
        double w = Double.parseDouble(wageField.getText());
        num1 = m ;
        num2 = w ;



        if (num1 <=0)
        {
            JOptionPane.showMessageDialog(null, "Please enter a time larger than 0", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }else if (num1 > 240)
        {
            JOptionPane.showMessageDialog(null, "You have worked to man mintues this week", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }else{

        }

            for(double i = 0; i< 3; i++)
            {


            }
            if(num2 < 0)
            {
                JOptionPane.showMessageDialog(null, "Please enter a wage", "Error", 
                        JOptionPane.ERROR_MESSAGE);
            }
            for(int i = 0; i < 3; i++)
            {


                for(count = 1; count <=3; count++){
                }
                tutorMin[0] = m;
                tutorWage[1] = w;

            timeField.setText("");
            wageField.setText("");
            wageField.requestFocus();
            timeField.requestFocus();
                }
        }
}
    private class runReportsButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double average = 0;
            double num1 = 0.00;
            double num2 = 0.00;
            double totalMin = 0.0;
            double totalEarnings = 0.0;
            final double MIN_WAGE = 7.25;
            double totalWage = 0.0;
            String wage = "";

            for (int i = 0; i < tutorMin.length; i++)
            {
                totalMin += i;
            }
            for (int j = 0; j < tutorWage.length; j++)
            {
                totalEarnings +=j;
            }
            for (int j = 0; j < tutorWage.length; j++)
            {
                totalWage +=j;
                average = totalEarnings;
            }
            if (average > MIN_WAGE)
            {
                wage = "Above Minimum Wage";
            }else if ((average < MIN_WAGE)&&(average > MIN_WAGE))
            {
                wage = "Equal to Minimum Wage";
            }else if (average < MIN_WAGE)
            {
                wage = "Below Minimum Wage";
            }

            String a = "Total mintues are " +totalMin +"\n";
            a+=("Average Earnings are:" + average + "\n");
            a+=("Minimum wage is: " +wage + "\n");
            textArea.setText(a);

            }}
    private class quitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
        public static void main (String[] args)
        {
            Tutorhelp th = new Tutorhelp();

        }
        }

Recommended Answers

All 5 Replies

When you see nothing happens in an interactive GUI, the first thing you should look for is the actionPerformed() method. The method is from ActionListener which is a class that keep listening any event from the frame/panel your program is being displayed. Currently, your method definition is calling to exit the program only. You need to implement it. An example of the method is here. You may need to take a close look at getActionCommand() method in order to know which button is clicked by a user.

The GUi does function,in the sense the actions from the button does cause the program to do Something however it doesn't use the user input numbers. it uses two defauts so it always displays
total mintues are 1.0
Average Earnings are 1.0
Minimum wage is : Below Minimum Wage

I cant see the error within the Actionlistener method

Sorry, my bad.

Look at your lines 136 for example, you are adding the value of i which is not supposed to be what you want. I believe you mean tutorMin[i].

Debugging by re-reading your code and thinking hard is very ineffective.
Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong.

Thank you so much! thats exactly what it was.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.