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 InvestmentFrame extends JFrame
13: {  
14:    public InvestmentFrame()
15:    {  
16:       account = new BankAccount(INITIAL_BALANCE);
17: 
18:       // Use instance fields for components 
19:       resultLabel = new JLabel(
20:             "balance=" + account.getBalance());
21: 
22:       // Use helper methods
23:       createRateField();
24:       createButton();
25:       createPanel();
26: 
27:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
28:    }
29: 
30:    public void createRateField()
31:    {
32:       rateLabel = new JLabel("Interest Rate: ");
33:       final int FIELD_WIDTH = 10;
34:       rateField = new JTextField(FIELD_WIDTH);
35:       rateField.setText("" + DEFAULT_RATE);
36:    }
37: 
38:    public void createButton()
39:    {  
40:       button = new JButton("Add Interest");
41: 
42:       class AddInterestListener implements ActionListener
43:       {
44:          public void actionPerformed(ActionEvent event)
45:          {
46:             double rate = Double.parseDouble(
47:                   rateField.getText());
48:             double interest = account.getBalance() 
49:                   * rate / 100;
50:             account.deposit(interest);
51:             resultLabel.setText(
52:                   "balance=" + account.getBalance());
53:          }            
54:       }
55: 
56:       ActionListener listener = new AddInterestListener();
57:       button.addActionListener(listener);
58:    }
59: 
60:    public void createPanel()
61:    {
62:       JPanel panel = new JPanel();
63:       panel.add(rateLabel);
64:       panel.add(rateField);
65:       panel.add(button);
66:       panel.add(resultLabel);      
67:       add(panel);
68:    }
69: 
70:    private JLabel rateLabel;
71:    private JTextField rateField;
72:    private JButton button;
73:    private JLabel resultLabel;
74:    private BankAccount account;
75: 
76:    private static final double DEFAULT_RATE = 10;
77:    private static final double INITIAL_BALANCE = 1000;
78: 
79:    private static final int FRAME_WIDTH = 500;
80:    private static final int FRAME_HEIGHT = 200;
81: }