1 import java.awt.Color;
  2 import java.awt.Container;
  3 import java.awt.Dimension;
  4 import java.awt.FlowLayout;
  5 import java.awt.Font;
  6 import java.awt.Graphics;
  7 import java.awt.GridLayout;
  8 import java.awt.Image;
  9 import java.awt.Toolkit;
 10 import java.awt.event.ActionEvent;
 11 import java.awt.event.ActionListener;
 12 import java.text.NumberFormat;
 13 
 14 import javax.swing.ImageIcon;
 15 import javax.swing.JButton;
 16 import javax.swing.JFrame;
 17 import javax.swing.JLabel;
 18 import javax.swing.JOptionPane;
 19 import javax.swing.JPanel;
 20 import javax.swing.JScrollPane;
 21 import javax.swing.JTextArea;
 22 import javax.swing.JTextField;
 23 import javax.swing.border.LineBorder;
 24 
 25 public class Fuli2  extends JFrame
 26 {
 27     private JLabel a1;
 28     private JLabel a2;
 29     private JLabel a3;
 30     private JLabel a4;
 31     private JLabel a5;
 32     private JTextField b1;
 33     private JTextField b2;
 34     private JTextField b3;
 35     private JTextField b4;
 36     private JTextField b5;
 37     private JButton c1;
 38     private JButton c2;
 39     private JButton c3;
 40     private JButton c4;
 41     private JButton c5;
 42     private JButton c6;
 43     private JTextArea text;
 44     public Fuli2()
 45     {
 46         creatComponents()    ;
 47         layoutComponents();
 48         registerHandlers();
 49         setTitle("存款应用程序");
 50         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 51         pack();
 52     }
 53     private void  creatComponents()    
 54     {
 55         a1=new JLabel("本  金 ");
 56         a1.setFont(new Font("宋体",Font.BOLD,18));
 57         a2=new JLabel("年利率 ");
 58         a2.setFont(new Font("宋体",Font.BOLD,18));
 59         a3=new JLabel("年 数 ");
 60         a3.setFont(new Font("宋体",Font.BOLD,18));
 61         a4=new JLabel("本息 ");
 62         a4.setFont(new Font("宋体",Font.BOLD,18));
 63         a5=new JLabel("月利率 ");
 64         a5.setFont(new Font("宋体",Font.BOLD,18));
 65         
 66         
 67         
 68         b1=new JTextField(10);//本金
 69         b2=new JTextField(10);//利率
 70         b3=new JTextField(10);//年数
 71         b4=new JTextField(10);//本息
 72         b5=new JTextField(10);//月利率
 73         c1=new JButton("复利");
 74         c1.setFont(new Font("宋体",Font.BOLD,15));
 75         c3=new JButton("单利");
 76         c3.setFont(new Font("宋体",Font.BOLD,15));
 77         c4=new JButton("定投");
 78         c4.setFont(new Font("宋体",Font.BOLD,15));
 79         c2=new JButton("清除");
 80         c2.setFont(new Font("宋体",Font.BOLD,15));
 81         c5=new JButton("复利存期/利率");
 82         c5.setFont(new Font("宋体",Font.BOLD,15));
 83         c6=new JButton("等额还款");
 84         c6.setFont(new Font("宋体",Font.BOLD,15));
 85         text=new JTextArea();
 86     }
 87     private void layoutComponents()
 88     {
 89         //Container c=this.getContentPane();    
 90         setLayout(new FlowLayout());
 91         JPanel panel1=new JPanel();
 92         panel1.add(a1);
 93         panel1.add(b1);
 94         
 95         JPanel panel2=new JPanel();
 96         panel2.add(a2);
 97         panel2.add(b2);
 98         panel2.add(a5);
 99         panel2.add(b5);
100         
101         JPanel panel3=new JPanel();
102         panel3.add(a3);
103         panel3.add(b3);
104         JPanel panel4=new JPanel();
105         panel4.add(a4);
106         panel4.add(b4);
107         
108         JPanel panel5=new JPanel();
109         panel5.add(c1);
110         panel5.add(c3);
111         panel5.add(c4);
112         panel5.add(c5);
113         panel5.add(c2);
114         panel5.add(c6);
115         JPanel  leftpanel=new JPanel(new GridLayout(6,2));
116         leftpanel.add(panel1);
117         leftpanel.add(panel2);
118         leftpanel.add(panel3);
119         leftpanel.add(panel4);
120         leftpanel.add(panel5);
121         JScrollPane panel6=new JScrollPane(text);
122         panel6.setPreferredSize(new Dimension(250,100));
123         add(leftpanel);
124         add(panel6);
125     }
126     private  void registerHandlers()
127     {
128          c1ActionEventHander hander1=new  c1ActionEventHander();
129          c1.addActionListener(hander1);
130          c2ActionEventHander hander2=new  c2ActionEventHander();
131          c2.addActionListener(hander2);
132     c3ActionEventHander hander3=new  c3ActionEventHander();
133         c3.addActionListener(hander3);
134         c4ActionEventHander hander4=new  c4ActionEventHander();
135         c4.addActionListener(hander4);
136         c5ActionEventHander hander5=new  c5ActionEventHander();
137         c5.addActionListener(hander5);
138         c6ActionEventHander hander6=new  c6ActionEventHander();
139         c6.addActionListener(hander6);
140     }
141     private class c1ActionEventHander implements ActionListener
142     {
143         public void actionPerformed(ActionEvent e)
144         {
145             double principal;
146             double amount;
147             double rate;
148             int n;
149             NumberFormat currencyformatter=NumberFormat.getCurrencyInstance();
150             String output="年"+"/t"+"复利存款"+"/n";
151              double year =1;
152            //  principal=Double.parseDouble(b1.getText());
153              rate=Double.parseDouble(b2.getText()); 
154              n=Integer.parseInt(b3.getText());
155              if(b1.getText().equals(""))//本金
156              {
157                  if (b4.getText().equals(""))
158                  {
159                      JOptionPane.showMessageDialog(null, "请输入本金或者本息");
160                 
161                  }
162                  else{
163                 amount = Double.parseDouble(b4.getText());
164                 principal=0;
165                 while (year <= n) {
166                     principal=amount/(Math.pow(1 + rate, year));
167                     year = year + 1;
168                 }
169                 
170                 output="本金"+currencyformatter.format(principal) + "\n";
171                 text.setText(output);
172             }
173         }
174         else {//本息
175             principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
176             
177             if (b4.getText().equals("")) {
178                 while (year <= n) {
179                     amount = principal * Math.pow(1 + rate, year);
180                     output += String.valueOf(year) + "\t"+ currencyformatter.format(amount) + "\n";
181                     year = year + 1;
182                 }
183 
184                 text.setText(output);
185                 
186             }
187             else{
188                 JOptionPane.showMessageDialog(null, "本金和本息输入一个即可");
189             }
190         }
191         }
192 
193 }
194     private class  c2ActionEventHander implements ActionListener
195     {
196         public void actionPerformed(ActionEvent e)
197         {
198             b1.setText("");
199             b2.setText("");
200             b3.setText("");
201             b4.setText("");
202             text.setText("");
203         }
204     }
205     private class c3ActionEventHander implements ActionListener {
206         public void actionPerformed(ActionEvent e) {
207             double principal;
208             double amount;
209             double rate;
210             int n;
211             NumberFormat currencyformatter = NumberFormat.getCurrencyInstance();
212             String output = "年" + "/" + "单利存款";
213             int year = 1;
214             
215             rate = Double.parseDouble(b2.getText());
216             n = Integer.parseInt(b3.getText());
217             if(b1.getText().equals(""))
218             {
219                 if (b4.getText().equals(""))
220                 {
221                     JOptionPane.showMessageDialog(null, "请输入本金或者本息");
222                     
223                 }
224                 else{
225                     amount = Double.parseDouble(b4.getText());
226                     principal=0;
227                     while (year <= n) {
228                         principal = amount / (1 + rate* year);
229                         year = year + 1;
230                     }
231                     
232                     output="本金"+currencyformatter.format(principal) + "\n";
233                     text.setText(output);
234                 }
235             }
236             else {
237                 principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
238                 
239                 if (b4.getText().equals("")) {
240                     while (year <= n) {
241                         amount = principal * (1 + rate* year);
242                         output += String.valueOf(year) + "\t"
243                                 + currencyformatter.format(amount) + "\n";
244                         year = year + 1;
245                     }
246     
247                     text.setText(output);
248                     }
249                 else{
250                     JOptionPane.showMessageDialog(null, "本金和本息输入一个即可");
251                 }
252             }
253             
254         }
255     }
256     private class c4ActionEventHander implements ActionListener
257     {
258     public void actionPerformed(ActionEvent e)
259     {
260          double principal;
261          double amount;
262          double rate;
263          int n;
264          NumberFormat currencyformatter=NumberFormat.getCurrencyInstance();
265          String output="年"+"/t"+"复利存款"+"/n";
266           double year =1;
267         //  principal=Double.parseDouble(b1.getText());
268           rate=Double.parseDouble(b2.getText()); 
269           n=Integer.parseInt(b3.getText());
270           principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
271           
272           if (b4.getText().equals("")) {
273               while (year <= n) {
274                   amount = principal*(Math.pow(1+ rate, year+1)-1)/rate;
275                   output += String.valueOf(year) + "\t"+ currencyformatter.format(amount) + "\n";
276                   year = year + 1;
277               }
278 
279               text.setText(output);
280               
281           }
282           else{
283               JOptionPane.showMessageDialog(null, "请输入每年定投的金额");
284           }
285         
286     }
287     }
288     private class c5ActionEventHander implements ActionListener//计算年份和利率
289     {
290         public void actionPerformed(ActionEvent e)
291         {
292             double amount;
293             double rate;
294             double principal;
295             String output;
296              NumberFormat currencyformatter = NumberFormat.getCurrencyInstance();
297             amount=Double.parseDouble(b4.getText());
298             principal=Double.parseDouble(b1.getText());
299            
300             if(b3.getText().equals("")){
301                 rate=Double.parseDouble(b2.getText());
302                 double year=(int)((Math.log(amount)/Math.log(1+rate))-(Math.log(principal)/Math.log(1+rate)));;
303                 output="年份"+currencyformatter.format(year) + "\n";
304                 text.setText(output);
305             }
306             else{
307                 if(b2.getText().equals("")){
308                     int year=Integer.parseInt(b3.getText());
309                     rate=((Math.pow(amount/principal, 1.0/year))-1);
310                     output="利率"+currencyformatter.format(rate) + "\n";
311                     text.setText(output);
312                 }
313                 else{
314                     JOptionPane.showMessageDialog(null, "利率和存期输入一个即可");
315                 }
316             }
317             
318         }
319     }
320     private class c6ActionEventHander implements ActionListener
321     {
322         public void actionPerformed(ActionEvent e)
323         {
324             double principal;
325             double amount;
326             double rate;
327             double trate;
328            
329             NumberFormat currencyformatter=NumberFormat.getCurrencyInstance();
330             String output="月"+"/t"+"等额还款"+"/n";
331             
332            //  principal=Double.parseDouble(b1.getText());
333              rate=Double.parseDouble(b5.getText()); 
334              double year=Integer.parseInt(b3.getText());
335              principal = Double.parseDouble(b1.getText()); // 字符串转化为数字
336                  if (b4.getText().equals("")) {
337                     
338                          rate=rate/12;
339                          trate=rate+(double)1;
340                          for(int i=1;i<year*12;i++){
341                              trate*=(rate+(double)1);
342                              
343                      }
344                      amount=(principal*trate*rate)/(trate-(double)1);
345                             output +="每月"+ currencyformatter.format(amount) + "\n";
346                             text.setText(output);
347                  }
348                  else{
349                      JOptionPane.showMessageDialog(null, "请输入贷款金额(本金)");
350                  }
351         }
352     }
353     public static void main(String[] args)
354     {
355          Fuli2 frame=new Fuli2();
356         frame.setVisible(true);
357         frame.setSize(600,450); 
358       frame.setResizable(false);
359     }
360 }

 

posted on 2016-03-21 14:03  nicole01  阅读(342)  评论(0编辑  收藏  举报