复利计算器v1

  1 public class MainFrame extends JFrame {
  2     
  3     /**
  4      * 文本框
  5      */
  6     private TextField[] texts = new TextField[5];
  7     
  8     /**
  9      * 标签
 10      */
 11     private JLabel[] labels = new JLabel[5];
 12     
 13     /**
 14      * 标签值
 15      */
 16     private String[] labelTexts = {"存入本金:","年利率(%):","存入年限:","年复利次数:","复利终值:"};
 17     
 18     /**
 19      * 确定按钮
 20      */
 21     private JButton jbtOk = new JButton("确定");
 22     
 23     /**
 24      * 退出按钮
 25      */
 26     private JButton jbtExit = new JButton("退出");
 27     
 28     /**
 29      * 利率获取时间的整数倍
 30      */
 31     private double N;
 32     
 33     /**
 34      * 复利终值
 35      */
 36     private double F;
 37     
 38     /**
 39      * 存入年限
 40      */
 41     private double Y;
 42     
 43     /**
 44      * 本金
 45      */
 46     private double P;
 47     
 48     /**
 49      * 利率
 50      */
 51     private double R;
 52     
 53     /**
 54      * 本金计算方式
 55      */
 56     private boolean pFlag;
 57     
 58     /**
 59      * 复利计算方式
 60      */
 61     private boolean fFlag;
 62     
 63     /**
 64      * 主窗体
 65      */
 66     public MainFrame() {
 67         this.setTitle("复利计算器");
 68         this.setBoundAtCenter();
 69         this.setResizable(true);
 70         //this.setLayout(new VFlowLayout());
 71         this.add(this.createTextPanel(), BorderLayout.CENTER);
 72         this.add(this.createButtonPanel(), BorderLayout.SOUTH);
 73         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 74         this.setVisible(true);
 75     }
 76 
 77     /**
 78      * 居中窗体
 79      */
 80     private void setBoundAtCenter() {
 81         Toolkit toolkit = Toolkit.getDefaultToolkit();
 82         Dimension dim = toolkit.getScreenSize();
 83         this.setSize(245, 350);
 84         int width = dim.width - this.getWidth() >> 1;
 85         int hight = (dim.height - this.getHeight() >> 1) - 32;
 86         this.setLocation(width, hight);
 87     }
 88     
 89     /**
 90      * 初始化文本框与标签位置以及标签值
 91      */
 92     private void initTextField() {
 93         int x = 90;
 94         int y = 50;
 95         int w = 130;
 96         int h = 20;
 97         for (int i = 0; i < texts.length; i++) {
 98             texts[i] = new TextField(x, y, w, h);
 99             labels[i] = new JLabel(labelTexts[i]);
100             labels[i].setBounds(x-80, y, w, h);
101             y += 28;
102         }
103     }
104     
105     /**
106      * 创建面板存放TextField和label
107      */
108     private JPanel createTextPanel() {
109         JPanel panel = new JPanel();
110         panel.setLayout(null);
111         this.initTextField();
112         for (int i = 0; i < texts.length; i++) {
113             panel.add(texts[i]);
114             panel.add(labels[i]);
115         }
116         return panel;
117     }
118     
119     /**
120      * 设置按钮以及监听
121      */
122     private JPanel createButtonPanel(){
123         
124         JPanel btnPanel = new JPanel(new VFlowLayout());
125         JPanel btnPanel1 = new JPanel(new FlowLayout());
126         
127         
128         //----------设置jbtOk的监听------------------
129         this.jbtOk.addActionListener(new ActionListener() {
130 
131             @Override
132             public void actionPerformed(ActionEvent e) {
133                 if(!pFlag) {
134                     texts[0].setText(String.valueOf(calculatePrincipal()));
135                 }else {
136                     texts[4].setText(String.valueOf(calculateCInterest()));
137                 }
138             }
139             
140         });
141         
142         btnPanel1.add(this.jbtOk);
143         
144         //----------设置jbtExit的监听------------------
145         this.jbtExit.addActionListener(new ActionListener() {
146             
147             @Override
148             public void actionPerformed(ActionEvent e) {
149                 // TODO Auto-generated method stub
150                 System.exit(EXIT_ON_CLOSE);
151                 
152             }
153         });
154         
155         btnPanel1.add(this.jbtExit);
156         //先添加ButtonGroup
157         btnPanel.add(this.createButtonGroup());
158         btnPanel.add(btnPanel1);
159         return btnPanel;
160     }
161     
162     /**
163      * 设置jradio按钮以及监听
164      */
165     private JPanel createButtonGroup() {
166         JPanel grpPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
167         ButtonGroup btnGrp = new ButtonGroup();
168         JRadioButton jrb1 = new JRadioButton("本金");
169         //----------设置jrb1的监听------------------
170         jrb1.addActionListener(new ActionListener() {
171             
172             @Override
173             public void actionPerformed(ActionEvent e) {
174                 pFlag = false;
175                 fFlag = true;
176                 changeType();
177             }
178         });
179         btnGrp.add(jrb1);
180         JRadioButton jrb2 = new JRadioButton("复利");
181         //----------设置jrb2的监听------------------
182         jrb2.addActionListener(new ActionListener() {
183             
184             @Override
185             public void actionPerformed(ActionEvent e) {
186                 pFlag = true;
187                 fFlag = false;
188                 changeType();
189             }
190         });
191         btnGrp.add(jrb2);
192         grpPanel.add(jrb1);
193         grpPanel.add(jrb2);
194         return grpPanel;
195     }
196     
197     private void changeType() {
198         this.texts[0].setEnabled(pFlag);
199         this.texts[4].setEnabled(fFlag);
200         if(pFlag) {
201             this.texts[4].setText("由此得出数值");
202             this.texts[0].setText(null);
203         } else {
204             this.texts[0].setText("由此得出数值");
205             this.texts[4].setText(null);
206         }
207     }
208 
209     /**
210      * F=P*(1+i)N(次方)
211      * F:复利终值
212      * P:本金
213      * R:利率
214      * N:利率获取时间的整数倍
215      * Y: 存入年限
216      * 
217      * 计算本金
218      */
219     private double calculatePrincipal(){
220         this.R = Double.parseDouble(this.texts[1].getText().trim());
221         this.Y = Double.parseDouble(this.texts[2].getText().trim());
222         this.N = Double.parseDouble(this.texts[3].getText().trim());
223         this.F = Double.parseDouble(this.texts[4].getText().trim());
224         System.out.println(Math.pow((1+R),N));
225         return F/Math.pow((1+R),N);
226         
227         
228     }
229     
230     /**
231      * 计算复利
232      */
233     private double calculateCInterest(){
234         this.P = Double.parseDouble(this.texts[0].getText().trim());
235         this.R = Double.parseDouble(this.texts[1].getText().trim());
236         this.Y = Double.parseDouble(this.texts[2].getText().trim());
237         this.N = Double.parseDouble(this.texts[3].getText().trim());
238         return P*Math.pow((1+R),N);
239     }
240 }
原文地址:https://www.cnblogs.com/peivxuan/p/5289741.html