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