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;
12: public class BankAccountView extends JPanel
13: implements Observer
14: {
15: private BankAccountModel model;
16: private BankAccountController controller;
17: private JButton depositButton = new JButton("Deposit");
18: private JButton withdrawButton = new JButton("Withdraw");
19: private JTextField amountField = new JTextField();
20: private JLabel balanceLabel = new JLabel();
23: // Constructor
24: public BankAccountView(BankAccountModel model)
25: {
26: this.model = model;
27: this.model.register(this);
28: this.controller = new BankAccountController(this, model);
29: buildUI();
30: }
33: // Called by model when the model changes
34: public void update()
35: {
36: balanceLabel.setText("Balance: " + model.getBalance());
37: }
40: // Provides access to the amount entered into the field
41: public double getAmount()
42: {
43: // Assume that the user entered a valid number
44: return Double.parseDouble(amountField.getText());
45: }
48: // Builds the display as seen by the user
49: private void buildUI()
50: {
51: setLayout(new BorderLayout());
52: JPanel buttons = new JPanel(new BorderLayout());
53: JPanel balance = new JPanel(new BorderLayout());
54: buttons.add(depositButton, BorderLayout.WEST);
55: buttons.add(withdrawButton, BorderLayout.EAST);
56: balance.add(balanceLabel, BorderLayout.NORTH);
57: balance.add(amountField, BorderLayout.SOUTH);
58: add(balance, BorderLayout.NORTH);
59: add(buttons, BorderLayout.SOUTH);
60: depositButton.addActionListener(controller);
61: withdrawButton.addActionListener(controller);
62: }
63: }