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