1
2
3
4
5
6
7
8import java.applet.Applet;
9import java.awt.Button;
10import java.awt.Color;
11import java.awt.GridLayout;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14
15
16
17public
18class ModelViewController2
19 extends Applet
20{
21 Button control1, control2;
22
23 Counter model;
24
25 ViewLabel view;
26 ViewColor view2;
27
28
29
30 public
31 void init() {
32
33
34
35
36 control1 = new Button("+1");
37 control1.setBackground(Color.orange);
38
39 control2 = new Button("-1");
40 control2.setBackground(Color.pink);
41
42 view = new ViewLabel();
43 view2 = new ViewColor();
44
45 model = new Counter();
46
47
48
49 setLayout(new GridLayout(4,1));
50
51 add(control1);
52 add(control2);
53 add(view);
54 add(view2);
55
56
57
58
59 control1.addActionListener
60 (new ActionListener()
61 {
62 public
63 void actionPerformed(ActionEvent e) {
64 model.incr(+1);
65 }
66 }
67 );
68
69 control2.addActionListener
70 (new ActionListener()
71 {
72 public
73 void actionPerformed(ActionEvent e) {
74 model.incr(-1);
75 }
76 }
77 );
78
79
80
81 model.addCounterChangedListener(view);
82 model.addCounterChangedListener(view2);
83
84
85 model.incr(0);
86
87 }
88}