Java Reference
In-Depth Information
private Account model;
private AccountWindow view;
public AccountController(Account model, AccountWindow view) {
this.model = model;
this.view = view;
}
public void addFunds(double amount) {
model.addFunds(amount);
view.updateView(model.getAmount());
}
public static void main(String args[]) {
Account myAccount = new Account("AimeeBartSeppe Ltd.", 3000);
AccountWindow myView = new AccountWindow();
AccountController controller = new AccountController(myAccount, myView);
myView.setController(controller);
myView.updateView(myAccount.getAmount());
myView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myView.setVisible(true);
}
}
And modify the view accordingly:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class AccountWindow extends JFrame {
private JTextField funds, add;
private JButton addButton;
private AccountController controller;
public AccountWindow() {
this.setSize(400, 120);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
funds = new JTextField(30);
add = new JTextField(30);
addButton = new JButton("Add funds");
funds.setEditable(false);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (controller != null)
Search WWH ::




Custom Search