/** * 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. */ /* ein ganz schlechtes Beispiel * fuer ein model-view-controller Programm * die Eingaben (controller), die Verarbeitung (model) * und die Ausgaben (view) sind beliebig vermischt. * Der Programmzustand, das model, wird in dem grafischen Objekt, * in dem label gespeichert. */ 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 ModelViewController00 extends Applet { Button control1, control2; 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"); //-------------------- // 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) { // auto unboxing int i = Integer.valueOf(view.getText()); view.setText("" + (i+1)); } } ); control2.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { int i = Integer.valueOf(view.getText()); view.setText("" + (i-1)); } } ); } }