2011-11-29 33 views
0
package loan; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class loan extends JApplet { 

    JLabel interestLabel = new JLabel("Interest Rate (e.g. 5.5 for 5.5%)"); 
    JLabel yearsLabel = new JLabel("Years"); 
    JLabel amountLabel = new JLabel("Loan Amount"); 
    JLabel monthlyLabel = new JLabel("Monthly Payment"); 
    JLabel totalLabel = new JLabel("Total Payment"); 
    JTextField jtfInterest = new JTextField(10); 
    JTextField jtfYears = new JTextField(10); 
    JTextField jtfAmount = new JTextField(10); 
    JTextField jtfMonthly = new JTextField(10); 
    JTextField jtfTotal = new JTextField(10); 
    JButton jbtCompute = new JButton("Compute Payment"); 

    public loan() { 

    JPanel panel1 = new JPanel(new GridLayout()); 
    panel1.setLayout(new GridLayout(5, 2, 5, 5)); 
    panel1.add(interestLabel); 
    panel1.add(jtfInterest); 
    panel1.add(yearsLabel); 
    panel1.add(jtfYears); 
    panel1.add(amountLabel); 
    panel1.add(jtfAmount); 
    panel1.add(monthlyLabel); 
    panel1.add(jtfMonthly); 
    panel1.add(totalLabel); 
    panel1.add(jtfTotal); 

    JPanel panel2 = new JPanel(new BorderLayout()); 
    panel2.add(jbtCompute, BorderLayout.EAST); 

    add(panel1, BorderLayout.NORTH); 
    add(panel2, BorderLayout.SOUTH); 

    jbtCompute.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       compute(); 
      } 
     }); 
    } 

    private void compute(){ 
     double monthlyInterestRate = Double.parseDouble(jtfInterest.getText())/1200; 
     double monthlyPayment = Double.parseDouble(jtfAmount.getText()) * monthlyInterestRate/
       (1 - (Math.pow(1/(1 + monthlyInterestRate), Double.parseDouble(jtfYears.getText()) * 12))); 
     double totalPayment = monthlyPayment * 12; 

     jtfMonthly.setText("" + monthlyPayment); 
     jtfTotal.setText("" + totalPayment); 


    } 

    public static void main (String args[]) { 
     JFrame frame = new JFrame(); 
     loan applet = new loan(); 
     frame.add(applet, BorderLayout.CENTER); 
     frame.setSize(300, 300); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

} 

我需要將它變成一個可以在網頁上運行的小程序。 我有問題的HTML文件,但那是另一回事。 我需要做些什麼來修改它,以便它既可以在網頁上運行,也可以在框架中作爲applet運行?把它變成一個可以在網頁上運行的小程序

+0

這是否拋出什麼異常?如果字符串字段包含除數字以外的任何內容,則應該拋出一些異常。 – Zohaib

+0

我用toString()代替了getText()。修復了這個問題。它在Double Class中拋出了一些瘋狂的錯誤,然後在最初嘗試解析的那一行。 – MISMajorDeveloperAnyways

回答

1

這樣做:

jtfInterest.getText() 
+0

謝謝。我想通了,但現在我必須將其轉換爲可在Web瀏覽器中使用。 – MISMajorDeveloperAnyways

相關問題