| | |
Writing Applet ActionListener Mortgage Calculator
Thread Solved |
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
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));
}
}
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));
}
}
In the action performed method, include all of your current code inside of an if statement that checks which button was clicked.
Example:
Example:
Java Syntax (Toggle Plain Text)
if(e.getSource==<name of button>) { //CODE GOES HERE. }
•
•
•
•
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:
Java Syntax (Toggle Plain Text)
if(e.getSource==<name of button>) { //CODE GOES HERE. }
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Shouldn't these be parsed:
T = Integer.parseInt(answerb.getText());
Also, don't you want to use doubles?
Java Syntax (Toggle Plain Text)
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?
•
•
Join Date: Nov 2005
Posts: 5
Reputation:
Solved Threads: 0
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));
}
}
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));
}
}
![]() |
Similar Threads
- Help with Mortgage Calculator (C++)
- Mortgage Calculator (C++)
Other Threads in the Java Forum
- Previous Thread: Nim Strategy
- Next Thread: Applet Rounding Issue
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






