954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Writing Applet ActionListener Mortgage Calculator

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));



}

}

anbarblue
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

In the action performed method, include all of your current code inside of an if statement that checks which button was clicked.

Example:

if(e.getSource==<name of button>)
{
//CODE GOES HERE.
}
Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

In the action performed method, include all of your current code inside of an if statement that checks which button was clicked.

Example:

if(e.getSource==<name of button>)
{
//CODE GOES HERE.
}

Also, make sure you added the actionlistener to the component.

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

Shouldn't these be parsed:

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

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


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


Also, don't you want to use doubles?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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));
}
}

anbarblue
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Also to incorporate doubles- would I instead do;

double Mort = 0;
verses
int Mort = 0;

anbarblue
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

You have semi-colons after the if, and else statements. They shouldn't be there.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Thanks for all your help everyone!! I figured it out! It's working now, except rounding. But I'll get it.

anbarblue
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You