Writing Applet ActionListener Mortgage Calculator

Thread Solved

Join Date: Nov 2005
Posts: 5
Reputation: anbarblue is an unknown quantity at this point 
Solved Threads: 0
anbarblue anbarblue is offline Offline
Newbie Poster

Writing Applet ActionListener Mortgage Calculator

 
0
  #1
Nov 27th, 2005
Here is my code so far. I'm not sure what I'm doing wrong, but I cannot get it to compile. Incompatible Types (Lines 90 - 92) . Please Please Help. My ultimate task is to write an Applet Mortgage Calculator.


import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class MortgageCalc extends JApplet implements ActionListener
{
Container con = getContentPane();




JLabel Greet = new JLabel("Estimate Mortgage Payments \n");
Font headlineFontg = new Font("Helvetica", Font.BOLD, 28);

JLabel Mortgage = new JLabel ("Amount of Mortgage: \n");
Font headlineFont = new Font("Helvetica", Font.BOLD, 12);
JTextField answer = new JTextField(10);


JLabel Interest= new JLabel("Interest Rate as % (e.g. 7.9): \n");
Font headlineFonta = new Font("Helvetica", Font.BOLD, 12);
JTextField answera = new JTextField(10);

JLabel Time = new JLabel("Number of Years: \n");
Font headlineFontb = new Font("Helvetica", Font.BOLD, 12);
JTextField answerb = new JTextField(10);

JLabel MonthPay = new JLabel("Monthly Payment: \n");
Font headlineFontc = new Font("Helvetica", Font.BOLD, 12);
JTextField MoPay = new JTextField(10);

JLabel MonthPaya = new JLabel(" - - ");
Font headlineFontd = new Font("Helvetica", Font.BOLD, 12);


JButton Calculate = new JButton("Calculate");
JButton Reset = new JButton("Reset");


public void init()
{

Greet.setFont(headlineFontg);
con.add(Greet);

Mortgage.setFont(headlineFont);
con.add(Mortgage);
con.add(answer);
con.setLayout(new FlowLayout());
answer.addActionListener(this);

Interest.setFont(headlineFonta);
con.add(Interest);
con.add(answera);
con.setLayout(new FlowLayout());
answera.addActionListener(this);

Time.setFont(headlineFontb);
con.add(Time);
con.add(answerb);
con.setLayout(new FlowLayout());
answerb.addActionListener(this);

MonthPay.setFont(headlineFontc);
con.add(MonthPay);
con.add(MoPay);
con.setLayout(new FlowLayout());
MoPay.addActionListener(this);

MonthPaya.setFont(headlineFontd);
con.add(MonthPaya);
con.setLayout(new FlowLayout());


con.add(Calculate);
con.add(Reset);

}


public void actionPerformed(ActionEvent e)
{
int Mort;
int Int;
int T;


Mort = answer.getText();
Int = answera.getText();
T = answerb.getText();

MonthPay.setText( "$" + (Int - (Mort * Int * T)) / (T*12));




}



}
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #2
Nov 27th, 2005
In the action performed method, include all of your current code inside of an if statement that checks which button was clicked.

Example:
  1. if(e.getSource==<name of button>)
  2. {
  3. //CODE GOES HERE.
  4. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #3
Nov 28th, 2005
Originally Posted by Ghost
In the action performed method, include all of your current code inside of an if statement that checks which button was clicked.

Example:
  1. if(e.getSource==<name of button>)
  2. {
  3. //CODE GOES HERE.
  4. }
Also, make sure you added the actionlistener to the component.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #4
Nov 28th, 2005
Shouldn't these be parsed:

  1. Mort = answer.getText();
  2. Int = answera.getText();
  3. T = answerb.getText();
  4.  
  5. MonthPay.setText( "$" + (Int - (Mort * Int * T)) / (T*12));

T = Integer.parseInt(answerb.getText());


Also, don't you want to use doubles?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: anbarblue is an unknown quantity at this point 
Solved Threads: 0
anbarblue anbarblue is offline Offline
Newbie Poster

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #5
Nov 28th, 2005
Thanks. I do have the actionListener as part of the my beg header. I put the code into the brackets. I added the parse, which I figured that out after I asked the question. (Can you see, I'm new new to Java). Do you see anything else wrong with it. Now it is saying 'else' without 'if'????? Here is what I have now??

public void actionPerformed(ActionEvent e)
{

if(e.getSource()==Reset);

int Mort = 0;
int Int = 0;
int T = 0;

else if(e.getSource()==Calculate);
{
Mort = Integer.parseInt(answer.getText());
Int = Integer.parseInt(answera.getText());
T = Integer.parseInt(answerb.getText());

MonthPay.setText( "$" + (Int - (Mort * Int * T)) / (T*12));
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: anbarblue is an unknown quantity at this point 
Solved Threads: 0
anbarblue anbarblue is offline Offline
Newbie Poster

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #6
Nov 28th, 2005
Also to incorporate doubles- would I instead do;

double Mort = 0;
verses
int Mort = 0;
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #7
Nov 28th, 2005
You have semi-colons after the if, and else statements. They shouldn't be there.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 5
Reputation: anbarblue is an unknown quantity at this point 
Solved Threads: 0
anbarblue anbarblue is offline Offline
Newbie Poster

Re: Writing Applet ActionListener Mortgage Calculator

 
0
  #8
Nov 29th, 2005
Thanks for all your help everyone!! I figured it out! It's working now, except rounding. But I'll get it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC