为什么我的计算没有出现在这个TextField中?

时间:2016-11-02 18:44:40

标签: java user-interface business-intelligence finance

我正在编写一个简单的程序,以便在Money Time of Money上获得乐趣,并且遇到了将我的变量calc发布到calculateTF TextField中的问题。

import java.util.Scanner;

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

// The time value of money (TVM) is the idea that money available at the present time 
// is worth more than the same amount in the future due to its potential earning capacity.

// FV = Future value of money
// PV = Present value of money
// i = interest rate
// n = number of compounding periods per year
// t = number of years

// FV = PV x (1 + (i / n))^(n x t)

public class timeValue2 extends JFrame {

    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;

    private JLabel futureValue, presentValue, interestRate, periods, years, payment;
    private JTextField futureValueTF, presentValueTF, interestRateTF, periodsTF, yearsTF, paymentTF, calculateTF;
    private JButton calculateB, exitB;

    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    public timeValue2() {

        // Labels
        futureValue = new JLabel("Enter the future value: ", SwingConstants.RIGHT);
        presentValue = new JLabel("Enter the present value: ", SwingConstants.RIGHT);
        interestRate = new JLabel("Enter I/Y: ", SwingConstants.RIGHT);
        periods = new JLabel("Enter periods: ", SwingConstants.RIGHT);
        years = new JLabel("Enter years: ", SwingConstants.RIGHT);
        payment = new JLabel("Enter payment: ", SwingConstants.RIGHT);

        // Text Fields
        futureValueTF = new JTextField(10);
        presentValueTF = new JTextField(10);
        interestRateTF = new JTextField(10);
        periodsTF = new JTextField(10);
        yearsTF = new JTextField(10);
        paymentTF = new JTextField(10);
        calculateTF = new JTextField(10);

        // Buttons
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        calculateB.addActionListener(cbHandler);
        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        exitB.addActionListener(ebHandler);

        setTitle("Time-Value of Money");

        Container pane = getContentPane();

        pane.setLayout(new GridLayout(4, 2));

        pane.add(futureValue);
        pane.add(futureValueTF);
        pane.add(presentValue);
        pane.add(presentValueTF);
        pane.add(interestRate);
        pane.add(interestRateTF);
        pane.add(payment);
        pane.add(paymentTF);
        pane.add(periods);
        pane.add(periodsTF);
        pane.add(years);
        pane.add(yearsTF);
        pane.add(calculateB);
        pane.add(calculateTF);
        pane.add(exitB);

        setSize(WIDTH, HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


    }

    private class CalculateButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            // Here the .getText() operator gets the text from TF pane's. 

            double futureValue, presentValue, interestRate, periods, years, calc, payment;
            String fv, pv, ir, p, y, pmt;

            fv = futureValueTF.getText();
            pv = presentValueTF.getText();
            ir = interestRateTF.getText();
            p = periodsTF.getText();
            y = yearsTF.getText();
            pmt = paymentTF.getText();

            futureValue = Double.parseDouble(fv);
            presentValue = Double.parseDouble(pv);
            interestRate = Double.parseDouble(ir);
            periods = Double.parseDouble(p);
            years = Double.parseDouble(y);
            payment = Double.parseDouble(pmt);

            // Now I need to perform the TVM operations. 

            // What I need is a document listener

            if(fv.isEmpty()) {

                calc = presentValue * Math.pow((1 + (interestRate / years)), (years * periods));
                calculateTF.setText("" + calc);

            } else if(pv.isEmpty()) {

                calc = ((payment/interestRate) - futureValue) * (1/Math.pow((1 + interestRate), years)) - (payment / interestRate);
                calculateTF.setText("" + calc);

            } else if(ir.isEmpty()) {

                calc = (presentValue * Math.pow((1 + interestRate), years)) + (payment * ((Math.pow((1 + interestRate), years) - 1)/interestRate)) + futureValue;
                calculateTF.setText("" + calc);

            } else if(p.isEmpty()) {

                calc = (presentValue * Math.pow((1 + interestRate),periods)) + (payment * (((Math.pow((1 + interestRate),periods)) - 1)/interestRate)) + futureValue;
                calculateTF.setText("" + calc);

            } else if(pmt.isEmpty()) {

                calc = (presentValue + ((presentValue + futureValue)/(Math.pow((1 + interestRate),periods) - 1) * (-interestRate / 1)));
                calculateTF.setText("" + calc);

            } 

            // The .setText() operator sets the text in the (yet to be created) calculate box. 

        }

    }

    public class ExitButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            System.exit(0);

        }

    }

    public static void main(String[] args) {

        timeValue2 boss = new timeValue2();

    }

}

运行时,代码会调出用户界面并允许用户输入数据,但显然计算没有运行或者值没有放在TextField中。

0 个答案:

没有答案