Java按揭计算器读取顺序文件

时间:2011-07-22 01:38:35

标签: java

  

可能重复:
  How do I populate jcombobox from a textfile?

好的,我在这里有一个问题,我无法安静地弄明白。我有一个文件在每行上有变量:5.35,5.5,5.75在一个名为apr.txt的文件中。我正在读取文件,需要将文件填充到JComboBox中。到目前为止,我已经将文件读取正常,但无法确定如何将其填充到JComboBox中。这一定很容易。有人能指点我正确的方向吗?

提前感谢您的帮助。

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;

import javax.swing.*;
import javax.swing.JComboBox;



  public class MortgageCalculatorGUI9 extends JFrame implements ActionListener
        {
         // Declarations

         //double [] Interest = {5.35, 5.5, 5.75};
         int [] Length = {7, 15, 30};
         double [] file_data = new double[3];


         JLabel mortgagelabel = new JLabel( "Principal Amount:$ " );
         JLabel paymentLabel = new JLabel( "Monthly Payment: " );
         JLabel intRateLabel = new JLabel( "Interest Rate %: " );
         JLabel termLabel = new JLabel( "Length of Loan of Loan in Years: "  );
         JTextField mortgagePrincipal = new JTextField(7);
         JTextField Payment = new JTextField(7);
         //JTextField intRateText = new JTextField(3);
         JComboBox intRateBox = new JComboBox();
         JTextField termText = new JTextField(3);
         JButton b7_535 = new JButton( "7 years at 5.35%" );
         JButton b15_55 = new JButton( "15 years at 5.50%" );
         JButton b30_575 = new JButton( "30 years at 5.75%");
         JButton exitButton = new JButton( "Exit" );
         JButton clearButton = new JButton( "Clear All" );
         JButton calculateButton = new JButton( "Calculate Loan" );
         JTextArea LoanPayments = new JTextArea(20,50);


        JScrollPane scroll = new JScrollPane(LoanPayments);

        public MortgageCalculatorGUI9()
        {
             //GUI setup
             super("Mortgage Calculator 1.0.5");
             setSize(800, 400);
             setLocation(500, 200);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             JPanel pane = new JPanel(new GridLayout(3,1)); Container grid = getContentPane();
             grid.setLayout(new GridLayout(4,0,4,4)); pane.add(grid);
             pane.add(scroll);
             grid.add(mortgagelabel);
             grid.add(mortgagePrincipal);
             grid.add(intRateLabel);
             //grid.add(intRateText);
             grid.add (intRateBox);
             grid.add(termLabel);
             grid.add(termText);
             grid.add(paymentLabel);
             grid.add(Payment);
             grid.add(b7_535);
             grid.add(b15_55);
             grid.add(b30_575);
             grid.add(calculateButton);
             grid.add(clearButton);
             grid.add(exitButton);
             Payment.setEditable(false);
             setContentPane(pane);
             setContentPane(pane);
             setVisible(true);

            //add GUI functionality
            calculateButton.addActionListener (this);
            exitButton.addActionListener(this);
            clearButton.addActionListener(this);
            b7_535.addActionListener(this);
            b15_55.addActionListener(this);
            b30_575.addActionListener(this);
            mortgagePrincipal.addActionListener(this);
            //intRateText.addActionListener(this);
            intRateBox.addActionListener (this);
            termText.addActionListener(this);
            Payment.addActionListener(this);

         }

            public void actionPerformed(ActionEvent e)
                 {
                    Object command = e.getSource();

                    if (command == exitButton)
                 {

                    System.exit(0);

                 }
                 else if (command == b7_535)
                 {

                    calcLoan(Length[0], file_data[0]);

                 }
                 else if (command == b15_55)
                 {

                    calcLoan(Length[1], file_data[1]);

                 }
                 else if (command == b30_575)
                 {

                    calcLoan(Length[2], file_data[2]);

                 }

                 else if (command == calculateButton )
                 {

                    double terms = 0;
                    double rates = 0;
                    try

                 {
                     terms = Double.parseDouble(termText.getText());
                     //rates = Double.parseDouble(intRateText.getText());
                     read_File ();
                 }
                    catch (Exception ex)
                 {
                    LoanPayments.setText("Invalid term or rate Amount");
                    return;
                 }

                    calcLoan(terms, rates);

                 }

                 else if (command == clearButton)
                 {

                     mortgagePrincipal.setText("");
                     Payment.setText("");
                     //intRateText.setText("");
                     termText.setText("");
                     LoanPayments.setText("");

                 }

             }


             //Input File

                public void read_File()
                {

                    File inFile = new File("apr.txt");

                    try
                    {

                        BufferedReader istream = new BufferedReader(new FileReader(inFile));

                        for(int x=0;x<6;x++)
                        {
                            file_data[x]=Double.parseDouble (istream.readLine());
                        }



                    }

                    catch (Exception ex)
                        {

                            LoanPayments.setText ("Could Not Read From File.");
                            return;

                        }
                }



                //this is what needs to be done
                 private void calcLoan(double terms, double rates)

                 {

                     termText.setText(String.valueOf(terms) );
                     //intRateText.setText(String.valueOf(rates));
                     double amount = 0;

                 try
                 {

                    amount = Double.parseDouble(mortgagePrincipal.getText());

                 }

                    catch (Exception ex)

                 {

                    LoanPayments.setText("Invalid mortgage Amount");
                    return;

                 }

                     double interestRate = rates;

                     // Calculations
                     double intRate = (interestRate / 100) / 12;
                     int Months = (int)terms * 12;
                     double rate = (intRate / 12);
                     double payment = amount * intRate / (1 - (Math.pow(1/(1 +  intRate), Months)));
                     double remainingPrincipal = amount;
                     double MonthlyInterest = 0;
                     double MonthlyAmt = 0;
                     double[] balanceArray = new double[Months];
                     double[] interestArray = new double[Months];
                     double[] monthArray = new double[Months];
                     NumberFormat Money =  NumberFormat.getCurrencyInstance();
                     Payment.setText(Money.format(payment));
                     LoanPayments.setText("Month\tPrincipal\tInterest\tEnding  Balance\n");
                     int currentMonth = 0;

                        while(currentMonth < Months)
                             {

                             //create loop calculations
                             MonthlyInterest = (remainingPrincipal * intRate);
                             MonthlyAmt = (payment - MonthlyInterest);
                             remainingPrincipal = (remainingPrincipal - MonthlyAmt);
                             LoanPayments.append((++currentMonth) + "\t" + Money.format(MonthlyAmt) + "\t" + Money.format(MonthlyInterest) + "\t" + Money.format(remainingPrincipal) + "\n");
                             balanceArray[currentMonth] = MonthlyAmt;
                             interestArray[currentMonth] = MonthlyInterest;
                             monthArray[currentMonth] = currentMonth;

                             }

                         }


            public static void main(String[] args)
             {

             MortgageCalculatorGUI9 frame= new MortgageCalculatorGUI9();

             }
 }

1 个答案:

答案 0 :(得分:1)

您是否看过JComboBox API的合适方法?如果你从那里开始,你可能会比在StackOverflow中询问更快得到正确的答案(暗示它以“add ...并以...... Item结尾”开头);)