/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /* das 1. Beispiel verbessert: * der Controller ist von dem Rest sauber entkoppelt * aber model und view sind noch vermischt */ import java.applet.Applet; import java.awt.Button; import java.awt.Label; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //-------------------- public class ModelViewController1 extends Applet { Button control1, control2; Counter1 model; Label view; //-------------------- public void init() { //-------------------- // die MVC Bestandteile initialisieren control1 = new Button("+1"); control1.setBackground(Color.orange); control2 = new Button("-1"); control2.setBackground(Color.pink); view = new Label(); view.setAlignment(Label.CENTER); view.setBackground(Color.gray); view.setText("0"); model = new Counter1(view); //-------------------- // die grafischen Objekte anordnen setLayout(new GridLayout(3,1)); add(control1); add(control2); add(view); //-------------------- // die controller Ereignisse behandeln control1.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { model.incr(+1); } } ); control2.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { model.incr(-1); } } ); } } //-------------------- class Counter1 { int cnt; Label l; // die Sicht wird hier im Konstruktor gesetzt public Counter1(Label l) { this.cnt = 0; this.l = l; } public void incr(int i) { cnt += i; // mitten im model eine Ausgabe !!! l.setText("" + cnt); } }