贷款计算器,等额本息
2021/10/3 23:13:15
本文主要是介绍贷款计算器,等额本息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
等额本息贷款计算器:
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 p1.add(new JLabel(" 本息合计 (元) ")); 98 p1.add(jtfTotalPayment); 99 p1.setBorder(new TitledBorder("输入利率、期限、贷款金额")); 100 101 JPanel p2=new JPanel(new BorderLayout()); 102 p2.add(jbtComputeLoan, BorderLayout.SOUTH); 103 104 JPanel jpNorth = new JPanel(new FlowLayout(FlowLayout.CENTER)); 105 //JPanel jpNorth = new JPanel(new BorderLayout()); 106 jpNorth.add(p1, BorderLayout.CENTER); 107 jpNorth.add(p2, BorderLayout.EAST); 108 109 //添加表格和滚动面板 110 table = new JTable();//引用模型,或table.setModel(model); 111 model = GetNewTableModel();//设置模型 112 table.setModel(model); 113 SetTableColumnDefaultWidth(table); 114 115 JScrollPane sc = new JScrollPane(table); 116 //getContentPane().add(sc, BorderLayout.CENTER); 117 //设置几个面板的方位布局 118 add(jpNorth,BorderLayout.NORTH); 119 add(sc, BorderLayout.CENTER); 120 jbtComputeLoan.addActionListener(new ButtonListener()); 121 } 122 123 void SetTableColumnDefaultWidth(JTable tb){ 124 // 第一列是贷款还款第几期,数字较小,列宽设置小一点更美观 125 tb.getColumnModel().getColumn(0).setPreferredWidth(7); 126 } 127 128 DefaultTableModel GetNewTableModel(){ 129 String[] columnNames = {"期次", "月供", "利息", "本金", "剩余欠款"};//定义表格列名 130 String[][] tableValues = {};//表格内容 131 DefaultTableModel m = new DefaultTableModel(tableValues, columnNames);//设置模型 132 return m; 133 } 134 135 public static void main(String[] args) { 136 LoanCalculator frame=new LoanCalculator(); 137 frame.setTitle("贷款计算器"); 138 frame.setSize(500,600); 139 //frame.pack(); 140 frame.setLocationRelativeTo(null); 141 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 142 frame.setVisible(true); 143 } 144 145 }
这篇关于贷款计算器,等额本息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-09程序员 SEO 系列:如何找到更多搜索关键词?
- 2024-11-09为何选择Spring AI Alibaba开发智能客服平台?
- 2024-11-09Sentinel不同的流控效果资料详解
- 2024-11-09Sentinel配置限流资料:新手入门教程
- 2024-11-09Sentinel配置限流资料详解
- 2024-11-09Sentinel熔断规则配置资料详解
- 2024-11-08Sentinel熔断规则配置资料详解
- 2024-11-08Sentinel限流资料入门教程
- 2024-11-08Springboot框架资料:新手入门与初级教程
- 2024-11-08Springboot框架资料:新手入门与初级应用教程