problem displaying calculation in JText field
this program is supposed to calculate and display monthly mortgage repayments and the total amount to be repaid. i'm only on the display monthly repayment part at the moment, and can't seem to get my figure to appear in the text field.
all help welcome.
Code:
package worksheet1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class CalculateMortgage extends JFrame implements ActionListener
{
private JTextField rate, year, loan, monthlyPay, totalPay;
private JButton calculate, clear;
private JPanel p1, p2;
Container c;
public CalculateMortgage()
{
//set window size, title and close operation
setSize(400, 200);
setResizable(false);
setTitle("Mortgage Application");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//initialise text fields and buttons
rate = new JTextField(10);
year = new JTextField(10);
loan = new JTextField(10);
monthlyPay = new JTextField(10);
totalPay = new JTextField(10);
calculate = new JButton("Compute Mortgage");
//initialise first panel
p1 = new JPanel();
//set grid layout with two columns and five rows
p1.setLayout(new GridLayout(5,2));
p1.add(new Label("Interest Rate"));
p1.add(rate);
p1.add(new Label("Years"));
p1.add(year);
p1.add(new Label("Loan Amount"));
p1.add(loan);
p1.add(new Label("Monthly Payment"));
p1.add(monthlyPay);
p1.add(new Label("Total Payment"));
p1.add(totalPay);
//initialise second panel
p2 = new JPanel();
//set flow layout to right
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
p2.add(calculate);
//place panels inside container and specify location
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
//register action listener
calculate.addActionListener(this);
}
//method to calculate pay
public void actionPerformed(ActionEvent e)
{
//if the button clicked is calculate
if(e.getActionCommand().equals(calculate))
{
//these variables = the value entered in the text fields
double interest = (Double.valueOf(rate.getText())).doubleValue();
int years = (Integer.valueOf(year.getText())).intValue();
double amount = (Double.valueOf(rate.getText())).doubleValue();
//calculate monthly repayments
double interestRate = interest / 10;
double totalAmount = years * amount;
double totalMonths = years * 12;
double monthlyRepayment = totalAmount * years * interestRate / totalMonths;
monthlyPay.setText(String.valueOf(monthlyRepayment));
}
}
public static void main(String args[])
{
CalculateMortgage one = new CalculateMortgage();
one.setVisible(true);
}
}