标签:
1 此版本的复利计算器对输入的数据进行了限制,只能输入数字字符,并对输入的数据进行判断是否完整 2 package JSQ; 3 4 import java.awt.EventQueue; 5 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 import javax.swing.border.EmptyBorder; 9 import javax.swing.ButtonGroup; 10 import javax.swing.JLabel; 11 import javax.swing.JOptionPane; 12 import javax.swing.JTextField; 13 import javax.swing.JButton; 14 15 import java.awt.event.ActionListener; 16 import java.awt.event.ActionEvent; 17 import java.awt.event.KeyAdapter; 18 import java.awt.event.KeyEvent; 19 import java.awt.Font; 20 import java.awt.event.MouseAdapter; 21 import java.awt.event.MouseEvent; 22 import java.awt.Color; 23 24 import javax.swing.JComboBox; 25 import javax.swing.DefaultComboBoxModel; 26 27 import java.awt.event.ItemListener; 28 import java.awt.event.ItemEvent; 29 30 import javax.swing.JRadioButton; 31 32 public class FuLiJiSuanQi extends JFrame { 33 34 /** 35 * 36 */ 37 private static final long serialVersionUID = 1L; 38 private JPanel contentPane; 39 private JTextField t2; 40 private JTextField t3; 41 private JTextField t1; 42 private JTextField t4; 43 private JLabel L3; 44 double capital,year,rate,sum; 45 double t_1,t_2,t_3; 46 int flag=1; 47 private JRadioButton r1; 48 private JRadioButton r2; 49 /** 50 * Launch the application. 51 */ 52 public static void main(String[] args) { 53 EventQueue.invokeLater(new Runnable() { 54 @Override 55 public void run() { 56 try { 57 FuLiJiSuanQi frame = new FuLiJiSuanQi(); 58 frame.setVisible(true); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 }); 64 } 65 66 /** 67 * Create the frame. 68 */ 69 @SuppressWarnings({ "unchecked", "rawtypes" }) 70 public FuLiJiSuanQi() { 71 setTitle("\u590D\u5229\u8BA1\u7B97\u56683.0"); 72 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 73 setBounds(100, 100, 474, 369); 74 contentPane = new JPanel(); 75 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 76 setContentPane(contentPane); 77 contentPane.setLayout(null); 78 79 final JLabel L1 = new JLabel("\u5B58\u6B3E(\u4E07):");//存款 80 L1.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 81 L1.setBounds(62, 85, 90, 15); 82 contentPane.add(L1); 83 84 t1 = new JTextField(); 85 t1.setBounds(166, 84, 173, 21); 86 contentPane.add(t1); 87 t1.setColumns(10); 88 t1.addKeyListener(new KeyAdapter(){ 89 public void keyTyped(KeyEvent e) { 90 int keyChar = e.getKeyChar(); 91 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 92 93 }else{ 94 e.consume(); //关键,屏蔽掉非法输入 95 } 96 } 97 }); 98 99 L3 = new JLabel("\u5229\u7387( %):");//利率 100 L3.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 101 L3.setBounds(62, 142, 81, 15); 102 contentPane.add(L3); 103 104 t2 = new JTextField(); 105 t2.setBounds(166, 115, 173, 21); 106 contentPane.add(t2); 107 t2.setColumns(10); 108 t2.addKeyListener(new KeyAdapter(){ 109 public void keyTyped(KeyEvent e) { 110 int keyChar = e.getKeyChar(); 111 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 112 113 }else{ 114 e.consume();//关键,屏蔽掉非法输入 115 } 116 } 117 }); 118 119 final JLabel L2 = new JLabel("\u672C\u606F(\u4E07):");//年限 120 L2.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 121 L2.setBounds(62, 117, 104, 15); 122 contentPane.add(L2); 123 124 t3 = new JTextField(); 125 t3.setBounds(166, 140, 173, 21); 126 contentPane.add(t3); 127 t3.setColumns(10); 128 t3.addKeyListener(new KeyAdapter(){ 129 public void keyTyped(KeyEvent e) { 130 int keyChar = e.getKeyChar(); 131 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 132 133 }else{ 134 e.consume(); //关键,屏蔽掉非法输入 135 } 136 } 137 }); 138 139 final JButton btnNewButton = new JButton("\u5E74\u9650(\u5E74)");//本息 140 t4 = new JTextField(); 141 t4.setEnabled(false); 142 t4.setForeground(new Color(127, 255, 212)); 143 t4.setBounds(166, 182, 173, 21); 144 contentPane.add(t4); 145 t4.setColumns(10); 146 final JComboBox comboBox = new JComboBox(); 147 comboBox.addItemListener(new ItemListener() { 148 @Override 149 public void itemStateChanged(ItemEvent e) { 150 if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5B58\u671F")) 151 { 152 flag=1; 153 L1.setText("\u5B58\u6B3E(\u4E07):"); 154 L2.setText("\u672C\u606F(\u4E07):"); 155 L3.setText("\u5229\u7387(%):"); 156 btnNewButton.setText("\u5E74\u9650(\u5E74)"); 157 t1.setText(null); 158 t2.setText(null); 159 t3.setText(null); 160 t4.setText(null); 161 } 162 else if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5B58\u989D")) { 163 flag=2; 164 L1.setText("\u672C\u606F(\u4E07)"); 165 L2.setText("\u5E74\u9650(\u5E74):"); 166 L3.setText("\u5229\u7387(%):"); 167 btnNewButton.setText("\u5B58\u6B3E(\u4E07)"); 168 t1.setText(null); 169 t2.setText(null); 170 t3.setText(null); 171 t4.setText(null); 172 } 173 else if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5229\u7387")) { 174 flag=3; 175 L1.setText("\u5B58\u6B3E(\u4E07):"); 176 L2.setText("\u672C\u606F(\u4E07):"); 177 L3.setText("\u5E74\u9650(\u5E74):"); 178 btnNewButton.setText("\u5229\u7387(%)"); 179 t1.setText(null); 180 t2.setText(null); 181 t3.setText(null); 182 t4.setText(null); 183 } 184 else { 185 flag=4; 186 L1.setText("\u5B58\u6B3E(\u4E07):"); 187 L2.setText("\u5E74\u9650(\u5E74):"); 188 L3.setText("\u5229\u7387(%):"); 189 btnNewButton.setText("\u672C\u606F(\u4E07)"); 190 t1.setText(null); 191 t2.setText(null); 192 t3.setText(null); 193 t4.setText(null); 194 } 195 } 196 }); 197 comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u4F30\u7B97\u5B58\u671F", "\u4F30\u7B97\u5B58\u989D", 198 "\u4F30\u7B97\u5229\u7387", "\u4F30\u7B97\u672C\u606F"})); 199 comboBox.setBounds(53, 10, 90, 21); 200 contentPane.add(comboBox); 201 202 r1 = new JRadioButton("\u5355\u5229"); 203 r1.setSelected(true); 204 r1.setBounds(6, 37, 64, 23); 205 contentPane.add(r1); 206 207 r2 = new JRadioButton("\u590D\u5229"); 208 r2.setBounds(72, 37, 70, 23); 209 contentPane.add(r2); 210 ButtonGroup group=new ButtonGroup(); 211 group.add(r1); 212 group.add(r2); 213 JLabel L = new JLabel("\u529F\u80FD"); 214 L.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); 215 L.setBounds(10, 13, 54, 15); 216 contentPane.add(L); 217 btnNewButton.addMouseListener(new MouseAdapter() { 218 @Override 219 public void mouseClicked(MouseEvent e) { 220 if (t1.getText().trim().equals("")||t2.getText().trim().equals("")||t3.getText().trim().equals("")) { 221 JOptionPane.showMessageDialog(contentPane, "请把数据输入完整"); 222 } 223 else { 224 getTitleValue(); 225 if(r1.isSelected()){ 226 calculate(); 227 } 228 else { 229 calculate_2(); 230 } 231 } 232 } 233 }); 234 btnNewButton.addActionListener(new ActionListener() { 235 @Override 236 public void actionPerformed(ActionEvent e) { 237 } 238 }); 239 btnNewButton.setBounds(53, 181, 90, 23); 240 contentPane.add(btnNewButton); 241 } 242 public void getTitleValue() { 243 t_1=Double.parseDouble(t1.getText()); 244 t_2=Double.parseDouble(t2.getText()); 245 t_3=Double.parseDouble(t3.getText()); 246 }; 247 public void calculate() { 248 switch (flag) { 249 case 1: 250 capital=t_1; 251 sum=t_2; 252 rate=t_3; 253 year=(sum/capital-1)/(rate/100); 254 t4.setText(Double.toString(year)); 255 break; 256 case 2: 257 sum=t_1; 258 year=t_2; 259 rate=t_3; 260 capital=sum/(year*rate/100+1); 261 t4.setText(Double.toString(capital)); 262 break; 263 case 3: 264 capital=t_1; 265 sum=t_2; 266 year=t_3; 267 rate=(sum/capital-1)/year*100; 268 t4.setText(Double.toString(rate)); 269 break; 270 case 4: 271 capital=t_1; 272 year=t_2; 273 rate=t_3; 274 sum=capital*(1+rate/100*year); 275 t4.setText(Double.toString(sum)); 276 break; 277 default: 278 break; 279 } 280 } 281 public void calculate_2() { 282 switch (flag) { 283 case 1: 284 capital=t_1; 285 sum=t_2; 286 rate=t_3; 287 year=Math.log(sum/capital)/Math.log(1+rate/100); 288 t4.setText(Double.toString(year)); 289 break; 290 case 2: 291 sum=t_1; 292 year=t_2; 293 rate=t_3; 294 capital=sum/Math.pow((1+rate/100),year); 295 t4.setText(Double.toString(capital)); 296 break; 297 case 3: 298 capital=t_1; 299 sum=t_2; 300 year=t_3; 301 rate=(Math.pow((sum/capital),1/year)-1)*100; 302 t4.setText(Double.toString(rate)); 303 break; 304 case 4: 305 capital=t_1; 306 year=t_2; 307 rate=t_3; 308 sum=capital*Math.pow((1+rate/100),year); 309 t4.setText(Double.toString(sum)); 310 break; 311 default: 312 break; 313 } 314 } 315 } 316 317
标签:
原文地址:http://www.cnblogs.com/LwF39/p/5296744.html