01: import java.awt.event.ActionEvent;
02: import java.awt.event.ActionListener;
03: import javax.swing.JButton;
04: import javax.swing.JFrame;
05: import javax.swing.JLabel;
06: import javax.swing.JPanel;
07: import javax.swing.JTextField;
08: 
09: /**
10:    This program displays the growth of an investment. 
11: */
12: public class InvestmentViewer2
13: {  
14:    public static void main(String[] args)
15:    {  
16:       JFrame frame = new JFrame();
17: 
18:       // The label and text field for entering the interest rate
19:       JLabel rateLabel = new JLabel("Interest Rate: ");
20: 
21:       final int FIELD_WIDTH = 10;
22:       final JTextField rateField = new JTextField(FIELD_WIDTH);
23:       rateField.setText("" + DEFAULT_RATE);
24: 
25:       // The button to trigger the calculation
26:       JButton button = new JButton("Add Interest");
27: 
28:       // The application adds interest to this bank account
29:       final BankAccount account = new BankAccount(INITIAL_BALANCE);
30: 
31:       // The label for displaying the results
32:       final JLabel resultLabel = new JLabel(
33:             "balance=" + account.getBalance());
34: 
35:       // The panel that holds the user interface components
36:       JPanel panel = new JPanel();
37:       panel.add(rateLabel);
38:       panel.add(rateField);
39:       panel.add(button);
40:       panel.add(resultLabel);      
41:       frame.add(panel);
42:   
43:       class AddInterestListener implements ActionListener
44:       {
45:          public void actionPerformed(ActionEvent event)
46:          {
47:             double rate = Double.parseDouble(
48:                   rateField.getText());
49:             double interest = account.getBalance() 
50:                   * rate / 100;
51:             account.deposit(interest);
52:             resultLabel.setText(
53:                   "balance=" + account.getBalance());
54:          }            
55:       }
56: 
57:       ActionListener listener = new AddInterestListener();
58:       button.addActionListener(listener);
59: 
60:       frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
61:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62:       frame.setVisible(true);
63:    }
64: 
65:    private static final double DEFAULT_RATE = 10;
66:    private static final double INITIAL_BALANCE = 1000;
67: 
68:    private static final int FRAME_WIDTH = 500;
69:    private static final int FRAME_HEIGHT = 200;
70: }