复利计算器3.1

时间:2024-03-09 11:50:41
此版本复利计算器 基本实现了所要求的全部功能,并对输入的数据进行出错处理了,而且对代码进行了整合与复用
package
JSQ import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.ButtonGroup; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.Font; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.Color; import javax.swing.JComboBox; import javax.swing.DefaultComboBoxModel; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import javax.swing.JRadioButton; public class FuLiJiSuanQi extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField t2; private JTextField t3; private JTextField t1; private JTextField t4; private JLabel L3; double capital,year,rate,sum; double t_1,t_2,t_3; int flag=1; private JRadioButton r1; private JRadioButton r2; KeyAdapter keyAdapter=new KeyAdapter(){ public void keyTyped(KeyEvent e) { int keyChar =e.getKeyChar(); if((keyChar>=KeyEvent.VK_0 &&keyChar<=KeyEvent.VK_9)||keyChar==KeyEvent.VK_PERIOD){ } else e.consume(); } }; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { FuLiJiSuanQi frame = new FuLiJiSuanQi(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public FuLiJiSuanQi() { setTitle("\u590D\u5229\u8BA1\u7B97\u56683.1"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 474, 369); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); final JLabel L1 = new JLabel("\u5B58\u6B3E(\u4E07):"); L1.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); L1.setBounds(62, 121, 90, 15); contentPane.add(L1); t1 = new JTextField(); t1.setBounds(166, 115, 173, 21); contentPane.add(t1); t1.setColumns(10); t1.addKeyListener(keyAdapter); L3 = new JLabel("\u5229\u7387( %):"); L3.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); L3.setBounds(62, 186, 81, 15); contentPane.add(L3); t2 = new JTextField(); t2.setBounds(166, 149, 173, 21); contentPane.add(t2); t2.setColumns(10); t2.addKeyListener(keyAdapter); final JLabel L2 = new JLabel("\u672C\u606F(\u4E07):"); L2.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); L2.setBounds(62, 155, 104, 15); contentPane.add(L2); t3 = new JTextField(); t3.setBounds(166, 180, 173, 21); contentPane.add(t3); t3.setColumns(10); t3.addKeyListener(keyAdapter); final JButton btnNewButton = new JButton("\u5E74\u9650(\u5E74)"); t4 = new JTextField(); t4.setEnabled(false); t4.setForeground(new Color(127, 255, 212)); t4.setBounds(166, 237, 173, 21); contentPane.add(t4); t4.setColumns(10); final JComboBox comboBox = new JComboBox(); comboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5B58\u671F")) { flag=1; r1.setEnabled(true); L1.setText("\u5B58\u6B3E(\u4E07):"); L2.setText("\u672C\u606F(\u4E07):"); L3.setText("\u5229\u7387(%):"); btnNewButton.setText("\u5E74\u9650(\u5E74)"); emptyJTextField(); } else if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5B58\u989D")) { flag=2; r1.setEnabled(true); L1.setText("\u672C\u606F(\u4E07)"); L2.setText("\u5E74\u9650(\u5E74):"); L3.setText("\u5229\u7387(%):"); btnNewButton.setText("\u5B58\u6B3E(\u4E07)"); emptyJTextField(); } else if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u5229\u7387")) { flag=3; r1.setEnabled(true); L1.setText("\u5B58\u6B3E(\u4E07):"); L2.setText("\u672C\u606F(\u4E07):"); L3.setText("\u5E74\u9650(\u5E74):"); btnNewButton.setText("\u5229\u7387(%)"); emptyJTextField(); } else if(comboBox.getSelectedItem().toString().equals("\u4F30\u7B97\u672C\u606F")){ flag=4; r1.setEnabled(true); L1.setText("\u5B58\u6B3E(\u4E07):"); L2.setText("\u5E74\u9650(\u5E74):"); L3.setText("\u5229\u7387(%):"); btnNewButton.setText("\u672C\u606F(\u4E07)"); emptyJTextField(); } else { flag=5; r1.setEnabled(false); r2.setSelected(isActive()); L1.setText("\u8D37\u6B3E(\u4E07)"); L2.setText("\u5E74\u9650(\u5E74):"); L3.setText("\u5229\u7387(%):"); btnNewButton.setText("\u7B49\u989D\u6708\u8FD8\u6B3E(\u4E07)"); emptyJTextField(); } } }); comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u4F30\u7B97\u5B58\u671F", "\u4F30\u7B97\u5B58\u989D", "\u4F30\u7B97\u5229\u7387","\u4F30\u7B97\u672C\u606F", "\u5206\u671F\u8FD8\u6B3E"})); comboBox.setBounds(53, 10, 113, 21); contentPane.add(comboBox); r1 = new JRadioButton("\u5355\u5229"); r1.setSelected(true); r1.setBounds(53, 60, 59, 23); contentPane.add(r1); r2 = new JRadioButton("\u590D\u5229"); r2.setBounds(114, 60, 71, 23); contentPane.add(r2); ButtonGroup group=new ButtonGroup(); group.add(r1); group.add(r2); JLabel L = new JLabel("\u529F\u80FD"); L.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); L.setBounds(10, 13, 54, 15); contentPane.add(L); btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (t1.getText().trim().equals("")||t2.getText().trim().equals("") ||t3.getText().trim().equals("")) { JOptionPane.showMessageDialog(contentPane, "\u8BF7\u628A\u6570\u636E\u8F93\u5165\u5B8C\u6574"); } else { getJTextFieldValue(); if(r1.isSelected()){ calculate(); } else { calculate_2(); } } } }); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); btnNewButton.setBounds(39, 236, 127, 23); contentPane.add(btnNewButton); JLabel label = new JLabel("\u65B9\u5F0F"); label.setFont(new Font("Microsoft JhengHei", Font.PLAIN, 16)); label.setBounds(10, 62, 54, 15); contentPane.add(label); } public void getJTextFieldValue() { t_1=Double.parseDouble(t1.getText()); t_2=Double.parseDouble(t2.getText()); t_3=Double.parseDouble(t3.getText()); }; public void emptyJTextField() { t1.setText(null); t2.setText(null); t3.setText(null); t4.setText(null); } public void calculate() { switch (flag) { case 1: capital=t_1; sum=t_2; rate=t_3; year=(sum/capital-1)/(rate/100); t4.setText(Double.toString(year)); break; case 2: sum=t_1; year=t_2; rate=t_3; capital=sum/(year*rate/100+1); t4.setText(Double.toString(capital)); break; case 3: capital=t_1; sum=t_2; year=t_3; rate=(sum/capital-1)/year*100; t4.setText(Double.toString(rate)); break; case 4: capital=t_1; year=t_2; rate=t_3; sum=capital*(1+rate/100*year); t4.setText(Double.toString(sum)); break; default: break; } } public void calculate_2() { switch (flag) { case 1: capital=t_1; sum=t_2; rate=t_3; year=Math.log(sum/capital)/Math.log(1+rate/100); t4.setText(Double.toString(year)); break; case 2: sum=t_1; year=t_2; rate=t_3; capital=sum/Math.pow((1+rate/100),year); t4.setText(Double.toString(capital)); break; case 3: capital=t_1; sum=t_2; year=t_3; rate=(Math.pow((sum/capital),1/year)-1)*100; t4.setText(Double.toString(rate)); break; case 4: capital=t_1; year=t_2; rate=t_3; sum=capital*Math.pow((1+rate/100),year); t4.setText(Double.toString(sum)); break; case 5: capital=t_1; year=t_2; rate=t_3; sum=capital*Math.pow((1+rate/1200),year*12)*(rate/1200) /(Math.pow((1+rate/1200),year*12)-1); t4.setText(Double.toString(sum)); break; default: break; } } }