复利计算2.0
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复利计算2.0相关的知识,希望对你有一定的参考价值。
1 package fulijisuan; 2 3 import java.awt.event.MouseAdapter; 4 import java.awt.event.MouseEvent; 5 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JLabel; 9 import javax.swing.JRadioButton; 10 import javax.swing.JTextField; 11 import javax.swing.SwingUtilities; 12 import javax.swing.UIManager; 13 14 import org.dyno.visual.swing.layouts.Constraints; 15 import org.dyno.visual.swing.layouts.GroupLayout; 16 import org.dyno.visual.swing.layouts.Leading; 17 import org.dyno.visual.swing.layouts.Trailing; 18 19 //VS4E -- DO NOT REMOVE THIS LINE! 20 public class text1 extends JFrame { 21 22 private static final long serialVersionUID = 1L; 23 private JButton jButton1; 24 private JButton jButton0; 25 private JLabel jLabel0; 26 private JLabel jLabel1; 27 private JLabel jLabel2; 28 private JTextField jTextField0; 29 private JTextField jTextField1; 30 private JTextField jTextField2; 31 private JLabel jLabel3; 32 private JLabel jLabel4; 33 private JButton jButton2; 34 private JTextField jTextField3; 35 private JTextField jTextField4; 36 private JRadioButton jRadioButton0; 37 public text1() { 38 initComponents(); 39 } 40 41 private void initComponents() { 42 setLayout(new GroupLayout()); 43 add(getJLabel0(), new Constraints(new Leading(34, 12, 12), new Leading(18, 10, 10))); 44 add(getJLabel1(), new Constraints(new Leading(33, 12, 12), new Leading(64, 12, 12))); 45 add(getJLabel2(), new Constraints(new Leading(32, 12, 12), new Leading(113, 10, 10))); 46 add(getJButton0(), new Constraints(new Leading(9, 12, 12), new Leading(259, 29, 10, 10))); 47 add(getJButton2(), new Constraints(new Leading(126, 10, 10), new Leading(259, 12, 12))); 48 add(getJButton1(), new Constraints(new Leading(239, 10, 10), new Leading(259, 12, 12))); 49 add(getJLabel4(), new Constraints(new Leading(30, 12, 12), new Leading(200, 10, 10))); 50 add(getJLabel3(), new Constraints(new Leading(30, 12, 12), new Leading(158, 10, 10))); 51 add(getJRadioButton0(), new Constraints(new Trailing(12, 109, 171), new Leading(62, 12, 12))); 52 add(getJRadioButton1(), new Constraints(new Leading(246, 10, 10), new Leading(131, 10, 10))); 53 add(getJTextField0(), new Constraints(new Leading(91, 95, 10, 10), new Leading(18, 10, 10))); 54 add(getJTextField2(), new Constraints(new Leading(91, 94, 12, 12), new Leading(109, 12, 12))); 55 add(getJTextField3(), new Constraints(new Leading(91, 94, 12, 12), new Leading(154, 12, 12))); 56 add(getJTextField4(), new Constraints(new Leading(90, 96, 12, 12), new Leading(198, 12, 12))); 57 add(getJTextField1(), new Constraints(new Leading(92, 93, 88, 88), new Leading(64, 24, 12, 12))); 58 setSize(320, 388); 59 } 60 61 private JRadioButton getJRadioButton1() { 62 if (jRadioButton1 == null) { 63 jRadioButton1 = new JRadioButton(); 64 jRadioButton1.setSelected(false); 65 jRadioButton1.setText("算本金"); 66 jRadioButton1.addMouseListener(new MouseAdapter() { 67 68 public void mouseClicked(MouseEvent event) { 69 jRadioButton1MouseMouseClicked(event); 70 } 71 }); 72 } 73 return jRadioButton1; 74 } 75 76 private JRadioButton getJRadioButton0() { 77 if (jRadioButton0 == null) { 78 jRadioButton0 = new JRadioButton(); 79 jRadioButton0.setSelected(false); 80 jRadioButton0.setText("算存款"); 81 jRadioButton0.addMouseListener(new MouseAdapter() { 82 83 public void mouseClicked(MouseEvent event) { 84 jRadioButton0MouseMouseClicked(event); 85 } 86 }); 87 } 88 return jRadioButton0; 89 } 90 91 private JTextField getJTextField4() { 92 if (jTextField4 == null) { 93 jTextField4 = new JTextField(); 94 } 95 return jTextField4; 96 } 97 98 private JTextField getJTextField3() { 99 if (jTextField3 == null) { 100 jTextField3 = new JTextField(); 101 } 102 return jTextField3; 103 } 104 105 private JButton getJButton2() { 106 if (jButton2 == null) { 107 jButton2 = new JButton(); 108 jButton2.setText("单利计算"); 109 jButton2.addMouseListener(new MouseAdapter() { 110 111 public void mouseClicked(MouseEvent event) { 112 jButton2MouseMouseClicked(event); 113 } 114 }); 115 } 116 return jButton2; 117 } 118 119 private JLabel getJLabel4() { 120 if (jLabel4 == null) { 121 jLabel4 = new JLabel(); 122 jLabel4.setText("利息:"); 123 } 124 return jLabel4; 125 } 126 127 private JLabel getJLabel3() { 128 if (jLabel3 == null) { 129 jLabel3 = new JLabel(); 130 jLabel3.setText("存款:"); 131 } 132 return jLabel3; 133 } 134 135 private JTextField getJTextField2() { 136 if (jTextField2 == null) { 137 jTextField2 = new JTextField(); 138 } 139 return jTextField2; 140 } 141 142 private JTextField getJTextField1() { 143 if (jTextField1 == null) { 144 jTextField1 = new JTextField(); 145 } 146 return jTextField1; 147 } 148 149 private JTextField getJTextField0() { 150 if (jTextField0 == null) { 151 jTextField0 = new JTextField(); 152 } 153 return jTextField0; 154 } 155 156 private JLabel getJLabel2() { 157 if (jLabel2 == null) { 158 jLabel2 = new JLabel(); 159 jLabel2.setText("年限:"); 160 } 161 return jLabel2; 162 } 163 164 private JLabel getJLabel1() { 165 if (jLabel1 == null) { 166 jLabel1 = new JLabel(); 167 jLabel1.setText("利率:"); 168 } 169 return jLabel1; 170 } 171 172 private JLabel getJLabel0() { 173 if (jLabel0 == null) { 174 jLabel0 = new JLabel(); 175 jLabel0.setText("本金:"); 176 } 177 return jLabel0; 178 } 179 180 private JButton getJButton0() { 181 if (jButton0 == null) { 182 jButton0 = new JButton(); 183 jButton0.setText("复利计算"); 184 jButton0.addMouseListener(new MouseAdapter() { 185 186 public void mouseClicked(MouseEvent event) { 187 jButton0MouseMouseClicked(event); 188 } 189 }); 190 } 191 return jButton0; 192 } 193 194 private JButton getJButton1() { 195 if (jButton1 == null) { 196 jButton1 = new JButton(); 197 jButton1.setText("清除"); 198 jButton1.addMouseListener(new MouseAdapter() { 199 200 public void mouseClicked(MouseEvent event) { 201 jButton1MouseMouseClicked(event); 202 } 203 }); 204 } 205 return jButton1; 206 } 207 208 private static void installLnF() { 209 try { 210 String lnfClassname = PREFERRED_LOOK_AND_FEEL; 211 if (lnfClassname == null) 212 lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName(); 213 UIManager.setLookAndFeel(lnfClassname); 214 } catch (Exception e) { 215 System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage()); 216 } 217 } 218 219 /** 220 * Main entry of the class. 221 * Note: This class is only created so that you can easily preview the result at runtime. 222 * It is not expected to be managed by the designer. 223 * You can modify it as you like. 224 */ 225 boolean suanfa = true ; 226 private JRadioButton jRadioButton1; 227 private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel"; 228 public static void main(String[] args) { 229 installLnF(); 230 SwingUtilities.invokeLater(new Runnable() { 231 public void run() { 232 text1 frame = new text1(); 233 frame.setDefaultCloseOperation(text1.EXIT_ON_CLOSE); 234 frame.setTitle("text1"); 235 frame.getContentPane().setPreferredSize(frame.getSize()); 236 frame.pack(); 237 frame.setLocationRelativeTo(null); 238 frame.setVisible(true); 239 } 240 }); 241 } 242 243 private void jButton0MouseMouseClicked(MouseEvent event) { 244 double p=0,i=0,f=0,j=0; 245 double s=1; 246 int n=0; 247 i = Double.parseDouble(jTextField1.getText()); 248 n = Integer.parseInt(jTextField2.getText()); 249 if(suanfa == false) 250 { 251 p = Double.parseDouble(jTextField0.getText()); 252 253 for(int a = 0;a < n;a++){ 254 s = s * (1 + i); 255 } 256 f = p * s; 257 j = f - p; 258 this.jTextField3.setText(String.format("%.2f", f)); 259 this.jTextField4.setText(String.format("%.2f", j)); 260 } 261 else{ 262 f = Double.parseDouble(jTextField3.getText()); 263 for(int a = 0;a<n;a++){ 264 s= (i+1)*s; 265 } 266 p=f*(1/s); 267 j=f-p; 268 269 this.jTextField0.setText(String.format("%.2f", p)); 270 this.jTextField4.setText(String.format("%.2f", j)); 271 } 272 } 273 274 private void jButton1MouseMouseClicked(MouseEvent event) { 275 this.jTextField0.setText(null); 276 this.jTextField1.setText(null); 277 this.jTextField2.setText(null); 278 this.jTextField3.setText(null); 279 this.jTextField4.setText(null); 280 } 281 282 private void jButton2MouseMouseClicked(MouseEvent event) { 283 double p=0,i=0,j=0,f=0;; 284 int n=0; 285 286 i = Double.parseDouble(jTextField1.getText()); 287 n = Integer.parseInt(jTextField2.getText()); 288 if(suanfa == false){ 289 p = Double.parseDouble(jTextField0.getText()); 290 j=p*i*n; 291 f=j+p; 292 this.jTextField3.setText(String.format("%.2f", f)); 293 this.jTextField4.setText(String.format("%.2f", j)); 294 } 295 else{ 296 f = Double.parseDouble(jTextField3.getText()); 297 p = f *(1/(1+i*n)); 298 j=f-p; 299 300 this.jTextField0.setText(String.format("%.2f", p)); 301 this.jTextField4.setText(String.format("%.2f", j)); 302 } 303 } 304 305 private void jRadioButton0MouseMouseClicked(MouseEvent event) { 306 suanfa = false; 307 jRadioButton1.setSelected(false); 308 } 309 310 private void jRadioButton1MouseMouseClicked(MouseEvent event) { 311 jRadioButton0.setSelected(false); 312 suanfa = true; 313 } 314 315 }
实现功能点:
1.输入本金、利率、年限复利、单利计算存款;
2.输入利率、年限、存款复利、单利计算本金;
3.一键清除输入框内数据。
运行结果:
以上是关于复利计算2.0的主要内容,如果未能解决你的问题,请参考以下文章