1 Replies - 1156 Views - Last Post: 23 June 2010 - 03:33 PM Rate Topic: -----

#1 SashaC   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 37
  • Joined: 08-April 10

No calculation with GUI calculator

Posted 23 June 2010 - 03:09 PM

I'm creating a GUI calculator. When I run the program the calculator shows up and I can enter data but the total does not appear. :helpsmilie:

The program has a separate class. Both parts are attached.

GUI Portion of program:
 /*
 * 
  */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
 *
 * @Programmer:
 */
public class GUICal extends JFrame implements ActionListener{

    /**
     * @param args the command line arguments
     */
    //instance variables
    private JTextField tf_mortAmount = new JTextField(9);
    private JTextField tf_interRate = new JTextField(7);
    private JTextField tf_term = new JTextField(3);
    private JTextField tf_payment = new JTextField(9);
    

    //Constructor to create and initalize submit button
    public GUICal ()
    {
        JButton submit = new JButton("Submit");
        submit.addActionListener(new SubmitListener());

        tf_mortAmount.addActionListener(new SubmitListener());
        tf_interRate.addActionListener(new SubmitListener());
        tf_term.addActionListener(new SubmitListener());
        tf_payment.setEditable(false);


        //Create pane to organize information and set layout
        JPanel info = new JPanel();
        info.setLayout(new FlowLayout() );

        //Add content to pane
        info.add(new JLabel("Mortgage Amount:")); //Add label for Mortgage Amount
        info.add (tf_mortAmount);//Text box to allow user to enter mortgage amount
        info.add(new JLabel ("Interest Rate:"));
        info.add(tf_interRate);
        info.add(new JLabel ("Loan Term in Years:"));
        info.add(tf_term);
        info.add (submit);
        info.add (new JLabel ("Monthly Mortgage Payment = $"));
        info.add (tf_payment);//Text box to output payment to user

        //Window attibutes
        setContentPane(info);
        pack();
        setTitle("Mortgage Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    class SubmitListener implements ActionListener
    {
        private Object submit;

        public void actionPerformed(ActionEvent e){
            Object source = e.getSource();
            if (source == submit)
            {
              try
              {
                  double principal = Double.parseDouble(tf_mortAmount.getText());
                  double monthlyInt = Double.parseDouble(tf_interRate.getText());
                  int years = Integer.parseInt(tf_term.getText());

                  GUICalCalculation calc = new GUICalCalculation (principal, years, monthlyInt);

                  if(principal>0&&years>0&&monthlyInt>0){
                      tf_payment.setText("" + calc.payment);

                  }
              }
                  catch(NumberFormatException nfe)
                  {
                      JOptionPane.showMessageDialog(null, "Please make sure all your entries are numeric!", "USER INPUT ERROR", JOptionPane.ERROR_MESSAGE);
                  }
              }
            }
           

          


    }
    public static void main(String[] args) {
        
        GUICal calculator = new GUICal();
        calculator.setVisible(true);
    }

}
 


Calculation portion
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.text.NumberFormat;
/**
 *
 * @author 
 */
public class GUICalCalculation {

    //member variables
    private double principal = -1.0;
    private double annualInterest = -1.0;
    private int years = -1;
   public double payment = -1;
    private NumberFormat fmt = NumberFormat.getInstance();
    private int monthlyInt;

    GUICalCalculation(double principal, int years, double monthlyInt) {
        this.principal = principal;
        this.monthlyInt = (int) ((monthlyInt/100)/12);
        this.years = years;


        fmt.setMaximumFractionDigits(2);
        fmt.setMinimumFractionDigits(2);

        calculatePayment();
                getPayment();
    }

    public void calculatePayment() {
        payment = (principal * monthlyInt)/ (1 - Math.pow(1/ (1 + monthlyInt), years *12));
    }
    public double getPayment(){
        return payment;
    }

}
 


Is This A Good Question/Topic? 0
  • +

Replies To: No calculation with GUI calculator

#2 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: No calculation with GUI calculator

Posted 23 June 2010 - 03:33 PM

Your logical error is here.

 private Object submit;

        public void actionPerformed(ActionEvent e){
            Object source = e.getSource();
            if (source == submit)
            {



Why have you got this Object submit that you are comparing? It should be the button submit.

So delete the Object variable..this bit here private Object submit;

Make your submit button an instance variable so you have a global scope like this JButton submit; and then instantiate it within the constructor.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1