这是复利计算器3.0版本,新加了一些功能,实现了基本的单复利计算,但还未对输入出错进行处理,下一个版本将更加完好
1 package JSQ; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 import javax.swing.border.EmptyBorder; 8 import javax.swing.ButtonGroup; 9 import javax.swing.JLabel; 10 import javax.swing.JTextField; 11 import javax.swing.JButton; 12 import java.awt.event.ActionListener; 13 import java.awt.event.ActionEvent; 14 import java.awt.Font; 15 import java.awt.event.MouseAdapter; 16 import java.awt.event.MouseEvent; 17 import java.awt.Color; 18 import javax.swing.JComboBox; 19 import javax.swing.DefaultComboBoxModel; 20 import java.awt.event.ItemListener; 21 import java.awt.event.ItemEvent; 22 import javax.swing.JRadioButton; 23 24 public class FuLiJiSuanQi extends JFrame { 25 26 /** 27 * 28 */ 29 private static final long serialVersionUID = 1L; 30 private JPanel contentPane; 31 private JTextField t2; 32 private JTextField t3; 33 private JTextField t1; 34 private JTextField t4; 35 private JLabel L3; 36 double capital,year,rate,sum; 37 int flag=1; 38 private JRadioButton r1; 39 private JRadioButton r2; 40 /** 41 * Launch the application. 42 */ 43 public static void main(String[] args) { 44 EventQueue.invokeLater(new Runnable() { 45 @Override 46 public void run() { 47 try { 48 FuLiJiSuanQi frame = new FuLiJiSuanQi(); 49 frame.setVisible(true); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } 53 } 54 }); 55 } 56 57 /** 58 * Create the frame. 59 */ 60 public FuLiJiSuanQi() { 61 setTitle("u590Du5229u8BA1u7B97u56683.0"); 62 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 63 setBounds(100, 100, 474, 369); 64 contentPane = new JPanel(); 65 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 66 setContentPane(contentPane); 67 contentPane.setLayout(null); 68 69 final JLabel L1 = new JLabel("u5B58u6B3E(u4E07):");//存款 70 L1.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 71 L1.setBounds(62, 85, 90, 15); 72 contentPane.add(L1); 73 74 t1 = new JTextField(); 75 t1.setBounds(166, 84, 173, 21); 76 contentPane.add(t1); 77 t1.setColumns(10); 78 79 L3 = new JLabel("u5229u7387( %):");//利率 80 L3.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 81 L3.setBounds(62, 142, 81, 15); 82 contentPane.add(L3); 83 84 t2 = new JTextField(); 85 t2.setBounds(166, 115, 173, 21); 86 contentPane.add(t2); 87 t2.setColumns(10); 88 89 final JLabel L2 = new JLabel("u672Cu606F(u4E07):");//年限 90 L2.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 91 L2.setBounds(62, 117, 104, 15); 92 contentPane.add(L2); 93 94 t3 = new JTextField(); 95 t3.setBounds(166, 140, 173, 21); 96 contentPane.add(t3); 97 t3.setColumns(10); 98 final JButton btnNewButton = new JButton("u5E74u9650(u5E74)");//本息 99 t4 = new JTextField(); 100 t4.setEnabled(false); 101 t4.setForeground(new Color(127, 255, 212)); 102 t4.setBounds(166, 182, 173, 21); 103 contentPane.add(t4); 104 t4.setColumns(10); 105 106 final JComboBox comboBox = new JComboBox(); 107 comboBox.addItemListener(new ItemListener() { 108 @Override 109 public void itemStateChanged(ItemEvent e) { 110 if(comboBox.getSelectedItem().toString().equals("u4F30u7B97u5B58u671F")) 111 { 112 flag=1; 113 L1.setText("u5B58u6B3E(u4E07):"); 114 L2.setText("u672Cu606F(u4E07):"); 115 L3.setText("u5229u7387(%):"); 116 btnNewButton.setText("u5E74u9650(u5E74)"); 117 t1.setText(null); 118 t2.setText(null); 119 t3.setText(null); 120 } 121 else if(comboBox.getSelectedItem().toString().equals("u4F30u7B97u5B58u989D")) { 122 flag=2; 123 L1.setText("u672Cu606F(u4E07)"); 124 L2.setText("u5E74u9650(u5E74):"); 125 L3.setText("u5229u7387(%):"); 126 btnNewButton.setText("u5B58u6B3E(u4E07)"); 127 t1.setText(null); 128 t2.setText(null); 129 t3.setText(null); 130 } 131 else if(comboBox.getSelectedItem().toString().equals("u4F30u7B97u5229u7387")) { 132 flag=3; 133 L1.setText("u5B58u6B3E(u4E07):"); 134 L2.setText("u672Cu606F(u4E07):"); 135 L3.setText("u5E74u9650(u5E74):"); 136 btnNewButton.setText("u5229u7387(%)"); 137 t1.setText(null); 138 t2.setText(null); 139 t3.setText(null); 140 } 141 else { 142 flag=4; 143 L1.setText("u5B58u6B3E(u4E07):"); 144 L2.setText("u5E74u9650(u5E74):"); 145 L3.setText("u5229u7387(%):"); 146 btnNewButton.setText("u672Cu606F(u4E07)"); 147 t1.setText(null); 148 t2.setText(null); 149 t3.setText(null); 150 } 151 } 152 }); 153 comboBox.setModel(new DefaultComboBoxModel(new String[] {"u4F30u7B97u5B58u671F", "u4F30u7B97u5B58u989D", "u4F30u7B97u5229u7387", "u4F30u7B97u672Cu606F"})); 154 comboBox.setBounds(53, 10, 90, 21); 155 contentPane.add(comboBox); 156 157 r1 = new JRadioButton("u5355u5229"); 158 r1.setSelected(true); 159 r1.setBounds(6, 37, 64, 23); 160 contentPane.add(r1); 161 162 r2 = new JRadioButton("u590Du5229"); 163 r2.setBounds(72, 37, 70, 23); 164 contentPane.add(r2); 165 ButtonGroup group=new ButtonGroup(); 166 group.add(r1); 167 group.add(r2); 168 JLabel L = new JLabel("u529Fu80FD"); 169 L.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 170 L.setBounds(10, 13, 54, 15); 171 contentPane.add(L); 172 btnNewButton.addMouseListener(new MouseAdapter() { 173 @Override 174 public void mouseClicked(MouseEvent e) { 175 if(r1.isSelected()){ 176 switch (flag) { 177 case 1: 178 capital=Double.parseDouble(t1.getText()); 179 sum=Double.parseDouble(t2.getText()); 180 rate=Double.parseDouble(t3.getText()); 181 year=(sum/capital-1)/(rate/100); 182 t4.setText(Double.toString(year)); 183 break; 184 case 2: 185 sum=Double.parseDouble(t1.getText()); 186 year=Double.parseDouble(t2.getText()); 187 rate=Double.parseDouble(t3.getText()); 188 capital=sum/(year*rate/100+1); 189 t4.setText(Double.toString(capital)); 190 break; 191 case 3: 192 capital=Double.parseDouble(t1.getText()); 193 sum=Double.parseDouble(t2.getText()); 194 year=Double.parseDouble(t3.getText()); 195 rate=(sum/capital-1)/year*100; 196 t4.setText(Double.toString(rate)); 197 break; 198 case 4: 199 capital=Double.parseDouble(t1.getText()); 200 year=Double.parseDouble(t2.getText()); 201 rate=Double.parseDouble(t3.getText()); 202 sum=capital*(1+rate/100*year); 203 t4.setText(Double.toString(sum)); 204 break; 205 default: 206 break; 207 } 208 } 209 else { 210 switch (flag) { 211 case 1: 212 capital=Double.parseDouble(t1.getText()); 213 sum=Double.parseDouble(t2.getText()); 214 rate=Double.parseDouble(t3.getText()); 215 year=Math.log(sum/capital)/Math.log(1+rate/100); 216 t4.setText(Double.toString(year)); 217 break; 218 case 2: 219 sum=Double.parseDouble(t1.getText()); 220 year=Double.parseDouble(t2.getText()); 221 rate=Double.parseDouble(t3.getText()); 222 capital=sum/Math.pow((1+rate/100),year); 223 t4.setText(Double.toString(capital)); 224 break; 225 case 3: 226 capital=Double.parseDouble(t1.getText()); 227 sum=Double.parseDouble(t2.getText()); 228 year=Double.parseDouble(t3.getText()); 229 rate=(Math.pow((sum/capital),1/year)-1)*100; 230 t4.setText(Double.toString(rate)); 231 break; 232 case 4: 233 capital=Double.parseDouble(t1.getText()); 234 year=Double.parseDouble(t2.getText()); 235 rate=Double.parseDouble(t3.getText()); 236 sum=capital*Math.pow((1+rate/100),year); 237 t4.setText(Double.toString(sum)); 238 break; 239 default: 240 break; 241 } 242 } 243 } 244 }); 245 btnNewButton.addActionListener(new ActionListener() { 246 @Override 247 public void actionPerformed(ActionEvent e) { 248 } 249 }); 250 btnNewButton.setBounds(53, 181, 90, 23); 251 contentPane.add(btnNewButton); 252 } 253 }