事件驱动器计算投资的未来值

/**
 *
 */
package Lyt;

import javax.swing.JFrame;

/**
 * @author Administrator
 *
 */
public class lyt {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  showNum frame=new showNum();
  frame.setTitle("未来值计算");
  frame.setSize(300,120);
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }

}

 

 

/**
 *
 */
package Lyt;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class showNum extends JFrame{
 JTextField Amount=new JTextField();
 JTextField Year=new JTextField();
 JTextField Rate=new JTextField();
 JTextField Future_value=new JTextField();
 JButton Caculate=new JButton("Caculate");
 public showNum()
 {
    JPanel p1=new JPanel(new GridLayout(5,2));
    p1.add(new JLabel("Investument Amount"));
    p1.add(Amount);
    p1.add(new JLabel("Year"));
    p1.add(Year);
    p1.add(new JLabel("Annual Interest Rate"));
    p1.add(Rate);
    p1.add(new JLabel("Future value"));
    p1.add(Future_value);
    p1.add(Caculate);
    add(p1);
    Caculate.addActionListener(new ButtonListener());
 }
 private class ButtonListener implements ActionListener {
 
  public void actionPerformed(ActionEvent e) {
   // TODO Auto-generated method stub
   double sum=0;
   double a=Double.parseDouble(Amount.getText());
   double b=Double.parseDouble(Year.getText());
   double c=Double.parseDouble(Rate.getText())/100.0/12;
   sum=a*Math.pow((1+c),b*12);
   Future_value.setText(String.format("%.2f", sum));
   
  }  
 }

 

}

 

 

 

 

 

 

posted on 2012-12-10 17:17  木本  阅读(137)  评论(0编辑  收藏  举报

导航