我有一个程序用于计算贷款的每月贷款支付,这是在JFrame内部,我已经调用了JButton来在用户输入后计算它。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
This program is used to calculate a monthly loan payment from a loan,
the user must input the loan amount, the interest rate in percentage,
and number of years, then return the monthly loan payment for the user.
Algorithm:
1. Create a JFrame to contain the java program
i. Frame has a width of 500, and a height of 500.
2. Create a JPanel where all the components will go into.
i. It should fit 2 JButtons, 4 JLabels, and 4 JTextFields.
3. Use JLabel and JTextField to create program components, which should include:
i. JLabel and JTextField for loan amount (for input).
ii. JLabel and JTextField for interest rate in years (for input).
iii. JLabel and JTextField for number of years for loan (for input).
iv. JLabel and JTextField for monthly loan payment (for output).
4. Use JButton to create 2 functions for the program, which are:
i. JButton to calculate monthly loan payment.
ii. JButton to quit out of the application.
5. Use ActionListener to create listener components for the 2 JButtons from step 4:
i. calcListener should contain the series of equations used to calculate a monthly loan payment,
then print out that result in the loan payment JTextField
ii. quitListener should activate functionality for the quit JButton.
*/
public class LoanCalc_Frame
{
private static ActionListener quitListener;
private static ActionListener calcListener;
public static void main(String[] args)
{
final int WIDTH = 500;
final int HEIGHT = 500;
JFrame frame;
JButton quitBtn, calcBtn;
JLabel amountLbl, interestLbl, yearsLbl, loanLbl;
final JTextField amountTf, interestTf, yearsTf, loanTf;
JPanel panel;
final ActionListener calcListener, quitListener;
frame = new JFrame ("Loan Payment Calculator");
panel = new JPanel();
amountLbl = new JLabel("Loan amount: ");
amountTf = new JTextField(10);
interestLbl = new JLabel("Interest Rate: ");
interestTf = new JTextField(10);
yearsLbl = new JLabel("Years: ");
yearsTf = new JTextField(10);
loanLbl = new JLabel("Monthly Loan Payment: ");
loanTf = new JTextField(10);
calcBtn = new JButton("Calculate Loan Payment");
quitBtn = new JButton("Quit");
class QuitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
quitListener = new QuitListener();
quitBtn.addActionListener(quitListener);
class CalcListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String aVal = amountTf.getText();
double amount = Double.parseDouble(aVal);
String iVal = interestTf.getText();
double ratepercent = Double.parseDouble(iVal);
String yVal = yearsTf.getText();
double years = Double.parseDouble(yVal);
double yearlyrate = (ratepercent / 100);
double monthlyrate = (yearlyrate / 12);
double months = (years * 12);
double monthlyInterestRate = (amount * monthlyrate) / (1 - Math.pow((1 + monthlyrate), (- months)));
String monthlyInterestRateStr = String.format("%2.2f\n", monthlyInterestRate);
loanTf.setText(monthlyInterestRateStr + "");
}
}
ActionListener Listener = new CalcListener();
calcBtn.addActionListener(Listener);
panel.add(amountLbl);
panel.add(amountTf);
panel.add(interestLbl);
panel.add(interestTf);
panel.add(yearsLbl);
panel.add(yearsTf);
panel.add(loanLbl);
panel.add(loanTf);
panel.add(calcBtn);
panel.add(quitBtn);
frame.add(panel);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我想为calcListener做的是让它调用我创建的贷款类的方法,但我不知道如何做到这一点。这是我创建的贷款类别:
public class Loan {
private double ratePercent;
private int years;
private double amount;
private java.util.Date loanDate;
/** Default constructor */
public Loan() {
this(7.5, 30, 100000);
}
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount) {
this.ratePercent = annualInterestRate;
this.years = numberOfYears;
this.amount = loanAmount;
loanDate = new java.util.Date();
}
/** Return annualInterestRate */
public double getRatePercent() {
return ratePercent;
}
/** Set a new annualInterestRate */
public void setRatePercent(double annualInterestRate) {
this.ratePercent = annualInterestRate;
}
/** Return numberOfYears */
public int getYears() {
return years;
}
/** Set a new numberOfYears */
public void setYears(int numberOfYears) {
this.years = numberOfYears;
}
/** Return loanAmount */
public double getAmount() {
return amount;
}
/** Set a newloanAmount */
public void setAmount(double loanAmount) {
this.amount = loanAmount;
}
/** Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = ratePercent / 1200;
return amount * monthlyInterestRate / (1 -
(Math.pow(1 / (1 + monthlyInterestRate), years * 12)));
}
/** Find total payment */
public double getTotalPayment() {
return getMonthlyPayment() * years * 12;
}
/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
}
有人能告诉我一个方法,以便我可以让Loan类方法进行calcListener的计算,而不是我编写的一系列方程式吗?
答案 0 :(得分:0)
我没有完全得到你想要的东西(与计算有关),但在这里你是实例化Loan类来调用方法的代码片段:
public void actionPerformed(ActionEvent event) {
String aVal = amountTf.getText();
double amount = Double.parseDouble(aVal);
String iVal = interestTf.getText();
double ratepercent = Double.parseDouble(iVal);
String yVal = yearsTf.getText();
int years = Integer.parseInt(yVal);
double yearlyrate = (ratepercent / 100);
double monthlyrate = (yearlyrate / 12);
double months = (years * 12);
//Instantiate the class to perform your calculation with the required aprams to do the calculation
Loan loan = new Loan(yearlyrate, years, amount);
// double monthlyInterestRate = (amount * monthlyrate) / (1 - Math.pow((1 + monthlyrate), (- months)));
String monthlyInterestRateStr = String.format("%2.2f\n", loan.getMonthlyPayment()); //Here you invoke the method you want to use to perform the calculation, I don't if you really want this value
loanTf.setText(monthlyInterestRateStr + "");
}