贷款计算器,等额本息

 

 

等额本息贷款计算器:

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 import javax.swing.border.*;
  5 import javax.swing.table.DefaultTableModel;
  6 
  7 public class LoanCalculator extends JFrame {
  8     private class ButtonListener implements ActionListener {
  9         @Override
 10         public void actionPerformed(ActionEvent e) {
 11             double interestRate=
 12                     Double.parseDouble(jtfAnnualInterestRate.getText());
 13             int year=
 14                     Integer.parseInt(jtfNumberOfYears.getText());
 15             double loanAmount=
 16                     Double.parseDouble(jtfLoanAmount.getText());
 17 
 18             double monthlyInterestRate=interestRate/1200;
 19             double monthlyPayment= loanAmount*monthlyInterestRate/
 20                     (1-1/Math.pow(1+monthlyInterestRate,year*12));
 21             double totalPayment=monthlyPayment*year*12;
 22             // 格式化显示,精确到小数点后2位
 23             jtfMonthlyPayment.setText(String.format("%.2f", monthlyPayment));
 24             jtfTotalPayment.setText(String.format("%.2f", totalPayment));
 25             //先清空表格中的数据
 26             model = GetNewTableModel();
 27             table.setModel(model);
 28             SetTableColumnDefaultWidth(table);
 29             CalculateMonthlyDetails(loanAmount,monthlyPayment,monthlyInterestRate,year*12);
 30         }
 31 
 32         // 计算每月返款金额中的本金和利息金额
 33         void CalculateMonthlyDetails(double loanAmount,double monthlyPay,double monthlyRate,int totalMonths)
 34         {
 35             double a = loanAmount;
 36             double p = monthlyPay;
 37             double r = monthlyRate;
 38             int monthIndex = 2;
 39             for (monthIndex = 1; monthIndex <= totalMonths; monthIndex++) {
 40                 // 最新的当前欠款金额
 41                 double curLoanLeft = GetLoanLeft(a,p,r,monthIndex-1);
 42                 double perLoanInterest = curLoanLeft*monthlyRate;
 43                 double perLoanPrincipal = monthlyPay - perLoanInterest;
 44                 // 还完本期月供后,还剩余的欠款金额
 45                 double loanLeft = GetLoanLeft(a,p,r,monthIndex);
 46                 String rowData[] = {""+monthIndex,
 47                         String.format("%.2f", monthlyPay),
 48                         String.format("%.2f", perLoanInterest),
 49                         String.format("%.2f", perLoanPrincipal),
 50                         String.format("%.2f", loanLeft) };
 51                 //在表格模型中增加一行内容
 52                 model.addRow(rowData);
 53 /*
 54                 System.out.print("期次:" + monthIndex);
 55                 System.out.print(", 月供 = " + String.format("%.2f", monthlyPay));
 56                 System.out.print(", 利息 = " + String.format("%.2f", perLoanInterest));
 57                 System.out.print(", 本金 = " + String.format("%.2f", perLoanPrincipal));
 58                 System.out.println(", 剩余 = " + String.format("%.2f", loanLeft));
 59 */
 60             }
 61         }
 62 
 63         // 计算出最新的剩余欠款金额
 64         double GetLoanLeft(double loanAmount,double monthlyPay,double monthlyRate,int monthIndex)
 65         {
 66             // 先计算出,第k期还款后,最新的当前欠款金额
 67             double loanLeft = loanAmount*Math.pow(1+monthlyRate,monthIndex)-
 68                     monthlyPay*(Math.pow(1+monthlyRate,monthIndex)-1)/monthlyRate;
 69             return loanLeft;
 70         }
 71     }
 72 
 73     private JTextField jtfAnnualInterestRate=new JTextField("4.41");
 74     private JTextField jtfNumberOfYears=new JTextField("30");
 75     private JTextField jtfLoanAmount=new JTextField("310000");
 76     private JTextField jtfMonthlyPayment=new JTextField(10);
 77     private JTextField jtfTotalPayment=new JTextField(10);
 78 
 79     private JButton jbtComputeLoan=new JButton("计算贷款");
 80 
 81     private DefaultTableModel model;//表格模型
 82     private JTable table;//表格
 83 
 84 
 85     public LoanCalculator(){
 86         JPanel p1=new JPanel(new GridLayout(5,2));
 87         JLabel jlb1 = new JLabel(" 贷款利率 (%) ");
 88         p1.add(jlb1);
 89         //jlb1.setHorizontalAlignment(JLabel.RIGHT);
 90         p1.add(jtfAnnualInterestRate);
 91         p1.add(new JLabel(" 贷款期限 (年) "));
 92         p1.add(jtfNumberOfYears);
 93         p1.add(new JLabel(" 贷款金额 (元) "));
 94         p1.add(jtfLoanAmount);
 95         p1.add(new JLabel(" 每月还款 (元) "));
 96         p1.add(jtfMonthlyPayment);
 97         jtfMonthlyPayment.setEnabled(false);
 98         p1.add(new JLabel(" 本息合计 (元) "));
 99         p1.add(jtfTotalPayment);
100         jtfTotalPayment.setEnabled(false);
101         p1.setBorder(new TitledBorder("输入利率、期限、贷款金额"));
102 
103         JPanel p2=new JPanel(new BorderLayout());
104         p2.add(jbtComputeLoan, BorderLayout.SOUTH);
105 
106         JPanel jpNorth = new JPanel(new FlowLayout(FlowLayout.CENTER));
107         //JPanel jpNorth = new JPanel(new BorderLayout());
108         jpNorth.add(p1, BorderLayout.CENTER);
109         jpNorth.add(p2, BorderLayout.EAST);
110 
111         //添加表格和滚动面板
112         table = new JTable();//引用模型,或table.setModel(model);
113         model = GetNewTableModel();//设置模型
114         table.setModel(model);
115         SetTableColumnDefaultWidth(table);
116 
117         JScrollPane sc = new JScrollPane(table);
118         //getContentPane().add(sc, BorderLayout.CENTER);
119         //设置几个面板的方位布局
120         add(jpNorth,BorderLayout.NORTH);
121         add(sc, BorderLayout.CENTER);
122         jbtComputeLoan.addActionListener(new ButtonListener());
123     }
124 
125     void SetTableColumnDefaultWidth(JTable tb){
126         // 第一列是贷款还款第几期,数字较小,列宽设置小一点更美观
127         tb.getColumnModel().getColumn(0).setPreferredWidth(7);
128     }
129 
130     DefaultTableModel GetNewTableModel(){
131         String[] columnNames = {"期次", "月供", "利息", "本金", "剩余欠款"};//定义表格列名
132         String[][] tableValues = {};//表格内容
133         DefaultTableModel m = new DefaultTableModel(tableValues, columnNames);//设置模型
134         return m;
135     }
136 
137     public static void main(String[] args) {
138         LoanCalculator frame=new LoanCalculator();
139         frame.setTitle("贷款计算器");
140         frame.setSize(500,600);
141         //frame.pack();
142         frame.setLocationRelativeTo(null);
143         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
144         frame.setVisible(true);
145     }
146 }

 

posted @ 2021-10-03 22:54  meteor119  阅读(322)  评论(0编辑  收藏  举报