2011-07-22 50 views
0

可能重複:
How do I populate jcombobox from a textfile?Java的房貸計算器閱讀順序文件

OK,我這裏有一個問題,我不能安靜弄明白。我有一個文件,其每行的變量爲: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

我剛剛搜索'填充jcombobox從文件'和瞧,[一個回答的問題](http://stackoverflow.com/questions/3173149/how-do-i-populate-jcombobox-from-a-textfile) - 這種搜索能力的缺乏是我們爲什麼抱怨。 –

回答

1

你看過JComboBox API的合適方法嗎?如果你從那裏開始,你可能會比在StackOverflow中詢問得到正確的答案(提示它以「添加...並以...結束於...項目」);)

+0

確實,這裏似乎缺乏自助。如果你第一次嘗試失敗,那麼SO不應該成爲去的地方。 –

+0

我是java編程新手,一直在研究這個問題。我已經得到這麼多,沒有太多的幫助來製作一個好看的應用程序。相信我,工作中的程序員也會受到我的考驗。我必須從某個地方開始,並且不知道在這個我將要結束的時候,所有的資源在哪裏。我非常感謝任何人爲我們提供的幫助,以及我知道這些寶貴的時間。我會嘗試建議的內容,看看我能得到多少。非常感謝你。 – David

+0

無論何時你使用api,它總是建議(或者是優先選擇)你在寫一行代碼之前通過那個api的文檔(如果有的話)... – buch11