package loan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class loan extends JApplet {
JLabel interestLabel = new JLabel("Interest Rate (e.g. 5.5 for 5.5%)");
JLabel yearsLabel = new JLabel("Years");
JLabel amountLabel = new JLabel("Loan Amount");
JLabel monthlyLabel = new JLabel("Monthly Payment");
JLabel totalLabel = new JLabel("Total Payment");
JTextField jtfInterest = new JTextField(10);
JTextField jtfYears = new JTextField(10);
JTextField jtfAmount = new JTextField(10);
JTextField jtfMonthly = new JTextField(10);
JTextField jtfTotal = new JTextField(10);
JButton jbtCompute = new JButton("Compute Payment");
public loan() {
JPanel panel1 = new JPanel(new GridLayout());
panel1.setLayout(new GridLayout(5, 2, 5, 5));
panel1.add(interestLabel);
panel1.add(jtfInterest);
panel1.add(yearsLabel);
panel1.add(jtfYears);
panel1.add(amountLabel);
panel1.add(jtfAmount);
panel1.add(monthlyLabel);
panel1.add(jtfMonthly);
panel1.add(totalLabel);
panel1.add(jtfTotal);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(jbtCompute, BorderLayout.EAST);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.SOUTH);
jbtCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
compute();
}
});
}
private void compute(){
double monthlyInterestRate = Double.parseDouble(jtfInterest.getText()) / 1200;
double monthlyPayment = Double.parseDouble(jtfAmount.getText()) * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), Double.parseDouble(jtfYears.getText()) * 12)));
double totalPayment = monthlyPayment * 12;
jtfMonthly.setText("" + monthlyPayment);
jtfTotal.setText("" + totalPayment);
}
public static void main (String args[]) {
JFrame frame = new JFrame();
loan applet = new loan();
frame.add(applet, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I need to turn this into an applet that can be run on a web page.
I am having issues with the html file, but that is another story.
What would I need to do to modify this so it can be run on both a web page, and as an applet in a frame?
Do this: