软件工程---复利计算8.0
代码如下:
import java.awt.Container; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class main extends JFrame implements ActionListener, ItemListener { private String msg[] = { "复利计算", "单利计算", "本金计算", "求期限", "求利率", "投资计算" }; Graphics g; private JButton JBfind = new JButton("确认"); private JButton JBclearMsg = new JButton("清除信息"); private Container con; private JComboBox<Object> JCBStart; private JComboBox<Object> JCBEnd; JTextField jP = new JTextField(); JTextField ji = new JTextField(); JTextField jn = new JTextField(); JTextField jm = new JTextField("1"); JTextField jF = new JTextField(); JLabel j1 = new JLabel("本金"); JLabel j2 = new JLabel("利率"); JLabel j3 = new JLabel("期限"); JLabel j4 = new JLabel("次数"); JLabel j5 = new JLabel("终值"); JLabel ti = new JLabel("复利计算器"); /** * @param args */ public main() { con = this.getContentPane(); // 选择框 JCBStart = new JComboBox<Object>(msg); JCBStart.setEditable(false);// 不可编辑 JCBStart.setBounds(10, 170, 135, 30); // 为选择框添加监听 // JCBStart.addItemListener(this); // JCBEnd.addItemListener(this); // 按钮位置,大小 JBfind.setBounds(220, 420, 90, 30); JBclearMsg.setBounds(320, 420, 90, 30); // 添加动作监听 JBfind.addActionListener(this); JBclearMsg.addActionListener(this); // 文字及显示面板 jP.setBounds(220, 170, 220, 30); ji.setBounds(220, 220, 220, 30); jn.setBounds(220, 270, 220, 30); jm.setBounds(220, 320, 220, 30); jF.setBounds(220, 370, 220, 30); j1.setBounds(180, 170, 30, 30); j2.setBounds(180, 220, 30, 30); j3.setBounds(180, 270, 30, 30); j4.setBounds(180, 320, 30, 30); j5.setBounds(180, 370, 30, 30); ti.setBounds(300, 50, 100, 100); // 显示信息 // 设置容器 con.setLayout(null); con.setSize(600, 600); con.setLocation(0, 0); // 把组件添加到容器中 con.add(JCBStart); con.add(JBfind); con.add(JBclearMsg); con.add(jF); con.add(ji); con.add(jm); con.add(jn); con.add(jP); con.add(j1); con.add(j2); con.add(j3); con.add(j4); con.add(j5); con.add(ti); this.setSize(600, 600); this.setLocation(300, 300); this.setResizable(false); // 窗体大小不可变 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗体结束程序 this.setVisible(true);// 显示窗体; } public static void main(String[] args) { // TODO Auto-generated method stub new main(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getActionCommand().equals("确认")) { double P=0; double i=0; double n=0; double m=0; double F=0; String select = (String) JCBStart.getSelectedItem(); if (select == "复利计算") { P = Double.valueOf(jP.getText().toString()).doubleValue(); i = Double.valueOf(ji.getText().toString()).doubleValue(); n = Double.valueOf(jn.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); F = P * (Math.pow((1 + i / m), n * m)); jF.setText(Double.toString(F)); } else if (select == "单利计算") { P = Double.valueOf(jP.getText().toString()).doubleValue(); i = Double.valueOf(ji.getText().toString()).doubleValue(); n = Double.valueOf(jn.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); F = P * (1 + i * n); jF.setText(Double.toString(F)); } else if (select == "本金计算") { i = Double.valueOf(ji.getText().toString()).doubleValue(); n = Double.valueOf(jn.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); F = Double.valueOf(jF.getText().toString()).doubleValue(); P = F / Math.pow((1 + i),n); jP.setText(Double.toString(P)); } else if (select == "求期限") { P = Double.valueOf(jP.getText().toString()).doubleValue(); i = Double.valueOf(ji.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); F = Double.valueOf(jF.getText().toString()).doubleValue(); n=Math.log((double)(F/P))/(Math.log((double)(1+i))); jn.setText(Double.toString(n)); } else if (select == "求利率") { P = Double.valueOf(jP.getText().toString()).doubleValue(); n = Double.valueOf(jn.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); F = Double.valueOf(jF.getText().toString()).doubleValue(); i =( Math.pow(F/P, 1.0 / n))-1; ji.setText(Double.toString(i)); } else if (select == "投资计算") { P = Double.valueOf(jP.getText().toString()).doubleValue(); i = Double.valueOf(ji.getText().toString()).doubleValue(); n = Double.valueOf(jn.getText().toString()).doubleValue(); m = Double.valueOf(jm.getText().toString()).doubleValue(); for (int j = 0; j < n*m; j++) { P = P * (1 + i); } F = P; jF.setText(Double.toString(F)); } } else if(e.getActionCommand().equals("清除信息")) { jF.setText(""); jP.setText(""); ji.setText(""); jn.setText(""); jm.setText("1"); } } @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub } }
运行结果:
<转载请标明出处及附上本网页的链接,谢谢>