2017-05-03 1 views
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); 

    } 

} 

은 내가 만든 대출 클래스에 대한 방법에 전화를 가지고,하지만 난 그렇게하는 방법을 모르겠어요. 난 그냥 대출 클래스 메소드를 가지고있는 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; 
    } 
} 

누군가가 나에게 길을 보여줄 수 있을까 : 여기에 내가 만든 대출 클래스입니다 대신에?

+1

그래서 만들려을 새로운'Loan' 오브젝트, 금액, ratepercent 및 years을 설정 한 후 매월 지불을 얻으시겠습니까? – immibis

+0

예,하지만 내 프로그램에서 amount, ratepercent 및 years는 JTextFields의 코드 시작 부분 ("amountTf" "interestTf"및 "yearsTF")에 나열된 내용과 동일합니다. –

+1

좋아, 분명히 JTextField에서 빠져 나가는 법을 알았으니 실제로 문제가 뭐니? – immibis

답변

0

내가 정확히 (계산 관련) 실현하려 싶지만 여기 메소드 호출하기 위해 대출 클래스를 인스턴스화하는 코드의 조각이다 것을 얻을하지 않습니다

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 + ""); 
} 
관련 문제