1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 5 import javax.swing.*; 6 7 public class Test_16_5 extends JFrame{ 8 private JTextField JT1 = new JTextField(); 9 private JTextField JT2 = new JTextField(); 10 private JTextField JT3 = new JTextField(); 11 private JTextField JT4 = new JTextField(); 12 private JLabel JL1 = new JLabel("investmentAmount"); 13 private JLabel JL2 = new JLabel("years"); 14 private JLabel JL3 = new JLabel("monthlyInterestRate"); 15 private JLabel JL4 = new JLabel("Future Value"); 16 private JButton JB = new JButton("Calculate"); 17 18 public Test_16_5(){ 19 JPanel JP1 = new JPanel(new GridLayout(4,2)); 20 JPanel JP2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 21 22 23 JP1.add(JL1);JP1.add(JT1); 24 JP1.add(JL2);JP1.add(JT2); 25 JP1.add(JL3);JP1.add(JT3); 26 JP1.add(JL4);JP1.add(JT4); 27 28 JP2.add(JB); 29 add(JP1,BorderLayout.CENTER); 30 add(JP2,BorderLayout.SOUTH); 31 32 JB.addActionListener(new ComputeListener()); 33 } 34 public static void main(String[] args) { 35 // TODO Auto-generated method stub 36 Test_16_5 T1 = new Test_16_5(); 37 T1.setTitle("Test_16.5"); 38 T1.pack(); 39 T1.setLocationRelativeTo(null); 40 T1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 41 T1.setVisible(true); 42 } 43 44 class ComputeListener implements ActionListener{ 45 46 @Override 47 public void actionPerformed(ActionEvent e) { 48 // TODO Auto-generated method stub 49 double investmentAmount = Double.parseDouble(JT1.getText()); 50 int years = Integer.parseInt(JT2.getText()); 51 double monthlyInterestRate = Double.parseDouble(JT3.getText()); 52 Interests interes = new Interests(investmentAmount,monthlyInterestRate,years); 53 JT4.setText(interes.getFutureValue()+""); 54 } 55 } 56 class Interests{ 57 private double inves; 58 private double monthly; 59 private int year; 60 public Interests(double inves,double monthly,int year){ 61 this.inves = inves; 62 this.year = year; 63 this.monthly = monthly; 64 } 65 public double getFutureValue(){ 66 return inves*Math.pow(1+monthly, year*12); 67 } 68 } 69 70 }
效果图:
原文:http://www.cnblogs.com/wanjiang/p/5636154.html