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 InvestmentViewer1
13: {  
14:    public static void main(String[] args)
15:    {  
16:       JFrame frame = new JFrame();
17: 
18:       // The button to trigger the calculation
19:       JButton button = new JButton("Add Interest");
20: 
21:       // The application adds interest to this bank account
22:       final BankAccount account = new BankAccount(INITIAL_BALANCE);
23: 
24:       // The label for displaying the results
25:       final JLabel label = new JLabel(
26:             "balance=" + account.getBalance());
27: 
28:       // The panel that holds the user interface components
29:       JPanel panel = new JPanel();
30:       panel.add(button);
31:       panel.add(label);      
32:       frame.add(panel);
33:   
34:       class AddInterestListener implements ActionListener
35:       {
36:          public void actionPerformed(ActionEvent event)
37:          {
38:             double interest = account.getBalance() 
39:                   * INTEREST_RATE / 100;
40:             account.deposit(interest);
41:             label.setText(
42:                   "balance=" + account.getBalance());
43:          }            
44:       }
45: 
46:       ActionListener listener = new AddInterestListener();
47:       button.addActionListener(listener);
48: 
49:       frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
50:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51:       frame.setVisible(true);
52:    }
53: 
54:    private static final double INTEREST_RATE = 10;
55:    private static final double INITIAL_BALANCE = 1000;
56: 
57:    private static final int FRAME_WIDTH = 400;
58:    private static final int FRAME_HEIGHT = 100;
59: }