天天看點

軟體工程---複利計算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

	}

}
      

  

運作結果:

軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0
軟體工程---複利計算8.0

<轉載請标明出處及附上本網頁的連結,謝謝>