public class BankAccountView extends JPanel
1: //BankAccountView.java
2: //Adapted from an example by Tony Sintes.
3: //The BankAccountView view is responsible for constructing the GUI
4: //and creating the associated controller.
6: import javax.swing.JPanel;
7: import javax.swing.JButton;
8: import javax.swing.JTextField;
9: import javax.swing.JLabel;
10: import java.awt.BorderLayout;
11: import java.util.Observer;
12: import java.util.Observable;
13: import java.text.*;
15: public class BankAccountView extends JPanel
16: implements Observer
17: {
18: private BankAccountModel model;
19: private BankAccountController controller;
20: private JButton depositButton = new JButton("Deposit");
21: private JButton withdrawButton = new JButton("Withdraw");
22: private JTextField amountField = new JTextField();
23: private JLabel balanceLabel = new JLabel();
26: //Constructor
27: public BankAccountView(BankAccountModel model)
28: {
29: model.addObserver(this);
30: this.model = model;
31: this.controller = new BankAccountController(this, model);
32: buildUI();
33: }
36: //Builds the display as seen by the user
37: private void buildUI()
38: {
39: setLayout(new BorderLayout());
40: JPanel buttons = new JPanel(new BorderLayout());
41: JPanel balance = new JPanel(new BorderLayout());
42: buttons.add(depositButton, BorderLayout.WEST);
43: buttons.add(withdrawButton, BorderLayout.EAST);
44: balance.add(balanceLabel, BorderLayout.NORTH);
45: balance.add(amountField, BorderLayout.SOUTH);
46: add(balance, BorderLayout.NORTH);
47: add(buttons, BorderLayout.SOUTH);
48: depositButton.addActionListener(controller);
49: withdrawButton.addActionListener(controller);
50: }
53: //Provides access to the amount entered into the field
54: public double getAmount()
55: {
56: return Double.parseDouble(amountField.getText());
57: }
58:
59: //Called by model when the model changes
60: public void update(Observable thing, Object o)
61: {
62: String balance = new String("");
63: balance += NumberFormat.getCurrencyInstance().
64: format(((BankAccountModel)thing).getBalance());
65: String displayText = " Balance: " + balance;
66: balanceLabel.setText(displayText);
67: }
68: }