      1  import java.awt.event.ActionEvent;

      2  import java.awt.event.ActionListener;

      3  import javax.swing.JButton;

      4  import javax.swing.JFrame;

      5  import javax.swing.JLabel;

      6  import javax.swing.JPanel;

      7  import javax.swing.JTextField;

      8

      9  /**

     10     This program displays the growth of an investment.

     11  */

     12  public class InvestmentViewer2

     13  {

     14     private static final int FRAME_WIDTH = 400;

     15     private static final int FRAME_HEIGHT = 100;

     16

     17     private static final double INTEREST_RATE = 10;

     18     private static final double INITIAL_BALANCE = 1000;

     19

     20     public static void main(String[] args)

     21     {

     22        JFrame frame = new JFrame();

     23

     24        // The button to trigger the calculation

     25        JButton button = new JButton("Add Interest");

     26

     27        // The application adds interest to this bank account

     28        final BankAccount account = new BankAccount(INITIAL_BALANCE);

     29

     30        // The label for displaying the results

     31        final JLabel label = new JLabel("balance: " + account.getBalance());

     32

     33        // The panel that holds the user interface components

     34        JPanel panel = new JPanel();

     35        panel.add(button);

     36        panel.add(label);

     37        frame.add(panel);

     38

     39        class AddInterestListener implements ActionListener

     40        {

     41           public void actionPerformed(ActionEvent event)

     42           {

     43              double interest = account.getBalance() * INTEREST_RATE / 100;

     44              account.deposit(interest);

     45              label.setText("balance: " + account.getBalance());

     46           }

     47        }

     48

     49        ActionListener listener = new AddInterestListener();

     50        button.addActionListener(listener);

     51

     52        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

     53        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     54        frame.setVisible(true);

     55     }

     56  }