I need some assistance with my calculator code. I have developed a fully funcitoning mortgage calculator up to this point. I have one small change that I want to do. Right now this is considered an applet and I want it to be considered an application.
It is my understanding that I need to move my opening from:
public class Mortgage extends JApplet {
to
public class Mortgage extends JFrame {
Doing so renders my calc inoperable. Any assistance is greatly appreciated.
Here is my simple code:
package Mortgage;
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.FileReader;
public class Mortgage extends JApplet {
private JLabel amountLabel=new JLabel("Loan amount: ");
private JTextField amount=new JTextField();
private JLabel termLabel=new JLabel("Term: ");
private JTextField term=new JTextField();
private JLabel rateLabel=new JLabel("Interest rate: ");
private JTextField rate=new JTextField();
private JComboBox termList;
private JLabel payLabel=new JLabel("Monthly Payment: ");
private JLabel payment=new JLabel();
private JButton calculate=new JButton("Calculate");
private JButton clear=new JButton("Clear");
private JButton exit=new JButton("Exit");
private JTextArea paymentSchedule=new JTextArea();
private JScrollPane schedulePane=new JScrollPane(paymentSchedule);
private Container cp = getContentPane();
private JPanel up=new JPanel();
private int[] trmArray;
double[] intrstArray;
//reading from sequential file
public void loadFile()
{
Reader fis;
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String[] line = b.readLine( ).split(",");
//Fill in term values
trmArray = new int[line.length];
for ( int i = 0; i < line.length; i++ )
{
trmArray[ i ] = Integer.parseInt(line[i].trim());
}
//Fill in rate values
line = b.readLine( ).split(",");
intrstArray = new double[line.length];
for ( int i = 0; i < line.length; i++ )
{
intrstArray[ i ] = Double.parseDouble(line[i].trim());
}
b.close();
fis.close();
}
catch ( Exception e1 )
{
e1.printStackTrace( );
}
}
public void init()
{
// Take care of termList
String[] terms= new String[trmArray.length];
for ( int i = 0; i < trmArray.length; i++ )
{
terms[i] = String.valueOf(trmArray[i]);
}
termList = new JComboBox(terms);
termList.setSelectedIndex(0);
termList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
// the source of the event is the combo box
int index = cb.getSelectedIndex();
String termYear = (String)cb.getSelectedItem();
term.setText(termYear);
rate.setText(intrstArray[index]+"");
}
});
// Take care of three buttons
calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// calculate the monthly payment
double p=Double.parseDouble(amount.getText());
double r=Double.parseDouble(rate.getText())/12;
double t=Integer.parseInt(term.getText())*12;
double monthlyPayment=p*Math.pow(1+(r/100),t)*(r/100)/(Math.pow(1+(r/100),t)-1);
DecimalFormat df = new DecimalFormat("$###,###.00");
payment.setText(df.format(monthlyPayment));
// calculate the detailed loan
double totalInterest = 0;
double balance=p;
double interest=0;
double principal=0;
int month;
StringBuffer buffer=new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int i=0; i<t; i++) {
month=i+1;
interest=balance*(r/100);
balance=balance+interest-monthlyPayment;
principal = monthlyPayment - interest;
buffer.append(month+"\t");
buffer.append(new String(df.format(principal))+"\t");
buffer.append(new String(df.format(interest))+"\t");
buffer.append(new String(df.format(balance))+"\n");
totalInterest+=interest;
}
paymentSchedule.setText(buffer.toString());
} catch(Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
amount.setText("");
payment.setText("");
paymentSchedule.setText("");
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
JPanel upScreen=new JPanel();
upScreen.setLayout(new GridLayout(5,2));
upScreen.add(amountLabel); upScreen.add(amount);
upScreen.add(termLabel); upScreen.add(term);
upScreen.add(new Label()); upScreen.add(termList);
upScreen.add(rateLabel); upScreen.add(rate);
upScreen.add(payLabel); upScreen.add(payment);
JPanel buttons=new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate); buttons.add(clear); buttons.add(exit);
//JPanel up=new JPanel();
up.setLayout(new BoxLayout(up, BoxLayout.Y_AXIS));
up.add(upScreen); up.add(buttons);
cp.add(BorderLayout.NORTH, up);
cp.add(BorderLayout.CENTER, schedulePane);
}
public static void main(String[] args) {
Mortgage applet = new Mortgage();
JFrame frame = new JFrame("Travis' Awesome Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().add(applet);
frame.setSize(400, 800);
applet.loadFile();
applet.init();
applet.start();
frame.setVisible(true);
}
}