码迷,mamicode.com
首页 > 编程语言 > 详细

Java 设计一个贷款计算器 简易

时间:2016-08-01 00:04:39      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 1 import javax.swing.*;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import javax.swing.border.*;
 5 
 6 public class LoanCalculator extends JFrame {
 7     private class ButtonListener implements ActionListener {
 8         @Override
 9         public void actionPerformed(ActionEvent e) {
10             // TODO Auto-generated method stub
11             double interest=
12                     Double.parseDouble(jtfAnnualInterestRate.getText());
13             int year=
14                     Integer.parseInt(jtfNumberOfYears.getText());
15             double loanAmount=
16                     Double.parseDouble(jtfLoanAmount.getText());
17             
18             double monthlyInterest=interest/1200;
19             double monthlyPayment= loanAmount*monthlyInterest/(1-1/Math.pow(1+monthlyInterest, 
20                     year*12));
21             double totalPayment=monthlyPayment*year*12;
22             
23             jtfMonthlyPayment.setText(String.format("%.2f", monthlyPayment));
24             jtfTotalPayment.setText(String.format("%.2f", totalPayment));
25         }
26     }
27 
28     private JTextField jtfAnnualInterestRate=new JTextField();
29     private JTextField jtfNumberOfYears=new JTextField();
30     private JTextField jtfLoanAmount=new JTextField();
31     private JTextField jtfMonthlyPayment=new JTextField();
32     private JTextField jtfTotalPayment=new JTextField();
33     
34     private JButton jbtComputeLoan=new JButton("Compute Payment");
35     
36     public LoanCalculator(){
37         JPanel p1=new JPanel(new GridLayout(5,2));
38         p1.add(new JLabel("Annual Interest Rate"));
39         p1.add(jtfAnnualInterestRate);
40         p1.add(new JLabel("Number of Years"));
41         p1.add(jtfNumberOfYears);
42         p1.add(new JLabel("Loan Amount"));
43         p1.add(jtfLoanAmount);
44         p1.add(new JLabel("Monthly Payment"));
45         p1.add(jtfMonthlyPayment);
46         p1.add(new JLabel("Total Payment"));
47         p1.add(jtfTotalPayment);
48         p1.setBorder(new TitledBorder("Enter loan amount, interest rate,"
49                 + " and year"));
50         
51         JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
52         p2.add(jbtComputeLoan);
53         add(p1,BorderLayout.CENTER);
54         add(p2,BorderLayout.SOUTH);
55         jbtComputeLoan.addActionListener(new ButtonListener());
56         
57     }
58 
59     public static void main(String[] args) {
60         // TODO Auto-generated method stub
61         LoanCalculator frame=new LoanCalculator();
62         frame.setTitle("LoanCalculator");
63         frame.pack();
64         frame.setLocationRelativeTo(null);
65         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66         frame.setVisible(true);
67     }
68 
69 }

 

技术分享

 

Java 设计一个贷款计算器 简易

标签:

原文地址:http://www.cnblogs.com/xingzhui/p/5724154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!