0
私は、JFrameの内部にあるローンの毎月のローン支払いを計算するためのプログラムを持っています。ユーザー入力の後に計算するためにJButtonを呼び出しました。私はcalcListenerのためにやってみたい何このクラスのメソッドをフレーム用に呼び出すにはどうすればよいですか?
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);
}
}
はそれが私が作成した融資クラスのメソッドを呼び出す必要がありますが、私はそれを行う方法がわからないです。ここに私が作成したローンクラスは次のとおりです。
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;
}
}
私はちょうどローンクラスのメソッドを持つことができるように、誰かが私に道を示すことができるだろうcalcListenerための計算ではなく、私が書かれている一連の方程式をください代わりに?
だから、あなたが作成したいです新しい「Loan」オブジェクトを作成し、金額、レート、年を設定し、毎月の支払いを取得しますか? – immibis
はい、私のプログラムでは、amount、ratepercent、およびyearsは、コードの冒頭にリストされたJTextFields(「amountTf」「interestTf」および「yearsTF」という名前のもの)にあるものです。 –
さて、明らかにJTextFieldからそれらを取り出す方法はわかっているので、実際には何が問題なのですか? – immibis