import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * A panel containing the input controls for one account */ public class Controller extends JPanel implements ActionListener { /** * The Model (Account) we are controlling */ private Model model; /** * Save these so actionPerformed() can access */ private JTextField textField; private JButton depositButton, withdrawButton; public Controller (Model model) { // Stash this.model = model; // Layout of our panel setBorder (new TitledBorder ("Controller")); setLayout (new GridLayout (0, 1, 20, 20)); // Our widget for entering amount of deposit or withdrawal textField = new JTextField ("0", 6); add (textField); // Our deposit button depositButton = new JButton ("Deposit"); depositButton.addActionListener (this); add (depositButton); // Withdraw button withdrawButton = new JButton ("Withdraw"); withdrawButton.addActionListener (this); add (withdrawButton); } /** * Common callback for both buttons, * we use ivar to disambiguate. */ public void actionPerformed (ActionEvent e) { if (e.getSource()==depositButton) model.deposit (Integer.parseInt (textField.getText())); else if (e.getSource()==withdrawButton) model.withdraw (Integer.parseInt (textField.getText())); } }