summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100257.html
blob: 8c32362290aca8c9779284e26646a59dca65c41a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100254.html;106,k100256.html;107,k100258.html;108,k100260.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100254.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100256.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100258.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100260.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">Kapitel 40 - Applets II
</table>
<hr>


<!-- Section -->
<a name="sectlevel2id040003"></a>
<h2>40.3 Umwandlung einer Applikation in ein Applet </h2>
<hr>
<ul>
<li><a href="k100257.html#sectlevel2id040003">40.3 Umwandlung einer Applikation in ein Applet</a>
<ul>
<li><a href="k100257.html#sectlevel3id040003001">40.3.1 Die Beispiel-Applikation</a>
<li><a href="k100257.html#sectlevel3id040003002">40.3.2 Variante 1: Das Programm als Popup-Fenster</a>
<li><a href="k100257.html#sectlevel3id040003003">40.3.3 Variante 2: Erstellen eines gleichwertigen Applets</a>
</ul>
</ul>
<hr>

<p>
In der Praxis steht man manchmal vor der Aufgabe, eine bestehende
Applikation in ein Applet umzuwandeln, etwa wenn ein vorhandendes
Programm in eine HTML-Pr&auml;sentation eingebunden werden soll. Da
die Unterschiede zwischen Applets und Applikationen nicht so gro&szlig;
sind, kann diese Konvertierung - etwas Gl&uuml;ck vorausgesetzt -
mit &uuml;berschaubarem Aufwand erledigt werden. Wir wollen in diesem
Abschnitt die prinzipielle Vorgehensweise erl&auml;utern und auf einige
der dabei m&ouml;glicherweise auftretenden Besonderheiten eingehen.


<!-- Section -->
<a name="sectlevel3id040003001"></a>
<h3>40.3.1 Die Beispiel-Applikation </h3>

<p>
Als Beispiel soll ein sehr einfaches Programm verwendet werden. Es
enth&auml;lt ein Textfeld mit einem Zahlenwert und vier Buttons, mit
denen die Zahl um 1, 10, 100 oder 1000 erh&ouml;ht werden kann. Die
Dialogelemente werden in einem <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
angeordnet, der von der <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>-Methode
erzeugt wird. Seine Gr&ouml;&szlig;e und Position werden ebenfalls
explizit vorgegeben: 
<a name="listingid040005"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Calculator.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Calculator
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<font color="#555555">008 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">009 </font>{
<font color="#555555">010 </font>  <font color="#0000AA">private</font> TextField tf;
<font color="#555555">011 </font>
<font color="#555555">012 </font>  <font color="#0000AA">public</font> Calculator()
<font color="#555555">013 </font>  {
<font color="#555555">014 </font>    <font color="#006699">super</font>(<font color="#0000FF">"Calculator"</font>);
<font color="#555555">015 </font>    addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">016 </font>    setBackground(Color.lightGray);
<font color="#555555">017 </font>    setLayout(<font color="#0000AA">new</font> GridLayout(2, 1));
<font color="#555555">018 </font>    tf = <font color="#0000AA">new</font> TextField(<font color="#0000FF">"777"</font>);
<font color="#555555">019 </font>    add(tf);
<font color="#555555">020 </font>    Panel p = <font color="#0000AA">new</font> Panel();
<font color="#555555">021 </font>    <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i &lt;= 1000; i *= 10) {
<font color="#555555">022 </font>      Button b = <font color="#0000AA">new</font> Button(<font color="#0000FF">"+"</font> + i);
<font color="#555555">023 </font>      b.addActionListener(<font color="#006699">this</font>);
<font color="#555555">024 </font>      p.add(b);
<font color="#555555">025 </font>    }
<font color="#555555">026 </font>    add(p);
<font color="#555555">027 </font>  }
<font color="#555555">028 </font>
<font color="#555555">029 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">030 </font>  {
<font color="#555555">031 </font>    String cmd = event.getActionCommand();
<font color="#555555">032 </font>    <font color="#006699">int</font> n1 = Integer.parseInt(tf.getText());
<font color="#555555">033 </font>    <font color="#006699">int</font> n2 = Integer.parseInt(cmd.substring(1));
<font color="#555555">034 </font>    tf.setText(<font color="#0000FF">""</font> + (n1 + n2));
<font color="#555555">035 </font>  }
<font color="#555555">036 </font>
<font color="#555555">037 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">038 </font>  {
<font color="#555555">039 </font>    Calculator calc = <font color="#0000AA">new</font> Calculator();
<font color="#555555">040 </font>    calc.setLocation(100, 100);
<font color="#555555">041 </font>    calc.setSize(200, 85);
<font color="#555555">042 </font>    calc.setVisible(<font color="#006699">true</font>);
<font color="#555555">043 </font>  }
<font color="#555555">044 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Calculator.java"><font color="#000055" size=-1>Calculator.java</font></a></td>
</tr>
</table>
<i>
Listing 40.5: Die Calculator-Applikation</i></p>

<p>
Die Ausgabe des Programms nach dem Starten sieht so aus: 
<p>
<a name="imageid040003"></a>
<img src="images/Calculator.gif">
<p>

<p><i>
Abbildung 40.3: Die Calculator-Applikation</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>

<p>
In vielen Beispielen in diesem Buch wird der Einfachheit halber die
in <a href="k100152.html#fensterschliessen">Abschnitt 23.2.4</a> vorgestellte
Klasse <a href="index_w.html#ixb100159"><font color=#000080><tt>WindowClosingAdapter</tt></font></a>
verwendet, um einen Listener zum Schlie&szlig;en des Fensters zu registrieren.
Damit ein solches Beispiel sich kompilieren l&auml;&szlig;t, muss
die Datei <font color="#660099">WindowClosingAdapter.java</font> im
aktuellen Verzeichnis vorhanden sein. Sie befindet sich auf der DVD
zum Buch oder in <a href="k100152.html#windowclosingadapter">Listing 23.2</a>.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF">&nbsp;Hinweis&nbsp;</font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>


<!-- Section -->
<a name="sectlevel3id040003002"></a>
<h3>40.3.2 Variante 1: Das Programm als Popup-Fenster </h3>

<p>
Die einfachste M&ouml;glichkeit, die Applikation als Applet laufen
zu lassen, besteht darin, die aus <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
abgeleitete Fensterklasse zu erhalten und aus dem Applet heraus als
eigenst&auml;ndiges Hauptfenster aufzurufen. Dazu schreiben wir ein
Hilfsapplet <font color="#000077"><tt>CalculatorApplet1</tt></font>,
das den ehemals in <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>
enthaltenen Code in der <a href="index_i.html#ixb101815"><font color=#000080><tt>init</tt></font></a>-Methode
ausf&uuml;hrt. Um das Fenster sichtbar zu machen, wird der Aufruf
von <font color="#000077"><tt>setVisible(true)</tt></font> in die
Methode <a href="index_s.html#ixb101110"><font color=#000080><tt>start</tt></font></a>
verlegt, und in <a href="index_s.html#ixb101115"><font color=#000080><tt>stop</tt></font></a>
wird <font color="#000077"><tt>setVisible(false)</tt></font> aufgerufen.
Das Fenster wird dadurch immer dann angezeigt, wenn die Seite mit
dem Applet im Browser sichtbar ist: 
<a name="listingid040006"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* CalculatorApplet1.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.applet.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> CalculatorApplet1
<font color="#555555">007 </font><font color="#0000AA">extends</font> Applet
<font color="#555555">008 </font>{
<font color="#555555">009 </font>  Calculator calc;
<font color="#555555">010 </font>
<font color="#555555">011 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> init()
<font color="#555555">012 </font>  {
<font color="#555555">013 </font>    calc = <font color="#0000AA">new</font> Calculator();
<font color="#555555">014 </font>    calc.setLocation(100, 100);
<font color="#555555">015 </font>    calc.setSize(200, 130);
<font color="#555555">016 </font>  }
<font color="#555555">017 </font>
<font color="#555555">018 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> start()
<font color="#555555">019 </font>  {
<font color="#555555">020 </font>    calc.setVisible(<font color="#006699">true</font>);
<font color="#555555">021 </font>  }
<font color="#555555">022 </font>
<font color="#555555">023 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> stop()
<font color="#555555">024 </font>  {
<font color="#555555">025 </font>    calc.setVisible(<font color="#006699">false</font>);
<font color="#555555">026 </font>  }
<font color="#555555">027 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/CalculatorApplet1.java"><font color="#000055" size=-1>CalculatorApplet1.java</font></a></td>
</tr>
</table>
<i>
Listing 40.6: Ein Applikations-Applet im Popup-Fenster</i></p>

<p>
Das Applet kann mit folgender HTML-Datei im Browser angezeigt werden:
<a name="listingid040007"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">&lt;html&gt;</font>
<font color="#555555">002 </font><font color="#0000AA">&lt;head&gt;</font>
<font color="#555555">003 </font><font color="#0000AA">&lt;title&gt;</font>CalculatorApplet1<font color="#0000AA">&lt;/title&gt;</font>
<font color="#555555">004 </font><font color="#0000AA">&lt;/head&gt;</font>
<font color="#555555">005 </font><font color="#0000AA">&lt;body&gt;</font>
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">&lt;h1&gt;</font>CalculatorApplet1<font color="#0000AA">&lt;/h1&gt;</font>
<font color="#555555">008 </font>
<font color="#555555">009 </font><font color="#0000AA">&lt;applet code=</font><font color="#0000FF">"CalculatorApplet1.class"</font><font color="#0000AA"> width=200 height=100&gt;</font>
<font color="#555555">010 </font>CalculatorApplet1
<font color="#555555">011 </font><font color="#0000AA">&lt;/applet&gt;</font>
<font color="#555555">012 </font>
<font color="#555555">013 </font><font color="#0000AA">&lt;/body&gt;</font>
<font color="#555555">014 </font><font color="#0000AA">&lt;/html&gt;</font></pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/CalculatorApplet1.html"><font color="#000055" size=-1>CalculatorApplet1.html</font></a></td>
</tr>
</table>
<i>
Listing 40.7: HTML-Datei zum Aufruf des Beispiel-Applets</i></p>

<p>
Die vorgeschlagene Methode ist einfach umzusetzen, hat aber einige
Nachteile: 
<ul>
<li>Eigenst&auml;ndige Hauptfenster werden im Browser mit einem zus&auml;tzlichen
Warnhinweis angezeigt (um das Erstellen von Trojanern zu erschweren).
Wir haben daher die H&ouml;he des Fensters von 85 auf 130 Pixel erh&ouml;ht.
<li>Der in dem <font color="#000077"><tt>WindowClosingAdapter</tt></font>
untergebrachte Aufruf zum Beenden des Programms beim Bet&auml;tigen
des Schlie&szlig;en-Buttons ist im Browser aus Sicherheitsgr&uuml;nden
nicht erlaubt und l&ouml;st eine <a name="ixa102979"><a href="index_a.html#ixb102207"><font color=#000080><tt>AppletSecurityException</tt></font></a></a>
aus. Auch <a href="index_d.html#ixb101296"><font color=#000080><tt>dispose</tt></font></a>
sollte nicht aufgerufen werden, da das Fenster unter Umst&auml;nden
noch einmal ben&ouml;tigt wird.
</ul>


<!-- Section -->
<a name="sectlevel3id040003003"></a>
<h3>40.3.3 Variante 2: Erstellen eines gleichwertigen Applets </h3>

<p>
Um den eigentlichen Vorteil von Applets, das <i>Einbetten</i> von
laufenden Programmen in HTML-Seiten, ausnutzen zu k&ouml;nnen, m&uuml;ssen
wir etwas mehr Aufwand treiben. Wir wollen uns dazu die Schritte ansehen,
um aus unserer Beispielapplikation ein gleichwertiges Applet zu machen:
<ul>
<li>Die Klassen <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
und <a href="index_a.html#ixb100588"><font color=#000080><tt>Applet</tt></font></a>
haben mit <a href="index_c.html#ixb101353"><font color=#000080><tt>Container</tt></font></a>
eine gemeinsame Basisklasse. Die Funktionalit&auml;t zum Hinzuf&uuml;gen
von Dialogelementen und Anordnen derselben mit Hilfe eines Layoutmanagers
steht also auch in <a href="index_a.html#ixb100588"><font color=#000080><tt>Applet</tt></font></a>
zur Verf&uuml;gung. Im ersten Schritt &auml;ndern wir daher einfach
die Basisklasse und leiten unser zuk&uuml;nftiges Applet von <a href="index_a.html#ixb100588"><font color=#000080><tt>Applet</tt></font></a>
ab. Zus&auml;tzlich muss das Paket <a href="index_j.html#ixb100593"><font color=#000080><tt>java.applet</tt></font></a>
importiert werden.
<li>Nun entfernen wir den Konstruktor und platzieren den dort befindlichen
Code in der <a href="index_i.html#ixb101815"><font color=#000080><tt>init</tt></font></a>-Methode.
Der Aufruf des Superklassenkonstruktors kann entfallen, weil ein Applet
keine Titelzeile hat. Auch alle eventuell vorhandenen Aufrufe von
<a href="index_s.html#ixb101376"><font color=#000080><tt>setTitle</tt></font></a>
m&uuml;ssen entfernt werden. Die Beschriftung des Applets kann in
der HTML-Datei erfolgen.
<li>Falls der <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
keinen expliziten Layoutmanager gesetzt hat, sondern mit dem vordefinierten
<a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
gearbeitet hat, muss ein Aufruf von <a href="index_s.html#ixb101597"><font color=#000080><tt>setLayout</tt></font></a>
erfolgen. <a href="index_a.html#ixb100588"><font color=#000080><tt>Applet</tt></font></a>
verwendet n&auml;mlich standardm&auml;&szlig;ig ein <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>.
<li>Nun m&uuml;ssen die Aufrufe der <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>-
bzw. <a href="index_w.html#ixb101146"><font color=#000080><tt>Window</tt></font></a>-spezifischen
Methoden entfernt werden. In unserem Beispiel betrifft das lediglich
<a name="ixa102980"><a href="index_s.html#ixb102208"><font color=#000080><tt>setWindowAdapter</tt></font></a></a>,
weitere Beispiele w&auml;ren <a href="index_s.html#ixb101556"><font color=#000080><tt>setMenuBar</tt></font></a>,
<a href="index_s.html#ixb101378"><font color=#000080><tt>setIconImage</tt></font></a>
oder <a href="index_s.html#ixb101641"><font color=#000080><tt>setResizable</tt></font></a>.
Am einfachsten ist es, das Programm in diesem Stadium zu kompilieren
und die Fehlermeldungen des Compilers auszuwerten.
<li>Nun sollte noch die <a href="index_m.html#ixb100150"><font color=#000080><tt>main</tt></font></a>-Methode
entfernt werden. Das Instanzieren des Applets und der Aufruf von <a href="index_s.html#ixb101147"><font color=#000080><tt>setVisible</tt></font></a>
k&ouml;nnen entfallen, denn sie werden vom Browser erledigt. Die Position
des Applets ergibt sich aus der HTML-Datei, und die Gr&ouml;&szlig;e
des Applets kann mit Hilfe der Parameter <a href="index_w.html#ixb102150"><font color=#000080><tt>WIDTH</tt></font></a>
und <a href="index_h.html#ixb102151"><font color=#000080><tt>HEIGHT</tt></font></a>
im APPLET-Tag festgelegt werden.
</ul>

<p>
Nachdem die Schritte beendet wurden, steht nun (wenigstens in diesem
nicht sehr komplexen Beispiel) ein gleichwertiges Applet zur Verf&uuml;gung:
<a name="listingid040008"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* CalculatorApplet2.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> java.applet.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> CalculatorApplet2
<font color="#555555">008 </font><font color="#0000AA">extends</font> Applet
<font color="#555555">009 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">010 </font>{
<font color="#555555">011 </font>  <font color="#0000AA">private</font> TextField tf;
<font color="#555555">012 </font>
<font color="#555555">013 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> init()
<font color="#555555">014 </font>  {
<font color="#555555">015 </font>    setBackground(Color.lightGray);
<font color="#555555">016 </font>    setLayout(<font color="#0000AA">new</font> GridLayout(2, 1));
<font color="#555555">017 </font>    tf = <font color="#0000AA">new</font> TextField(<font color="#0000FF">"777"</font>);
<font color="#555555">018 </font>    add(tf);
<font color="#555555">019 </font>    Panel p = <font color="#0000AA">new</font> Panel();
<font color="#555555">020 </font>    <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i &lt;= 1000; i *= 10) {
<font color="#555555">021 </font>      Button b = <font color="#0000AA">new</font> Button(<font color="#0000FF">"+"</font> + i);
<font color="#555555">022 </font>      b.addActionListener(<font color="#006699">this</font>);
<font color="#555555">023 </font>      p.add(b);
<font color="#555555">024 </font>    }
<font color="#555555">025 </font>    add(p);
<font color="#555555">026 </font>  }
<font color="#555555">027 </font>
<font color="#555555">028 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">029 </font>  {
<font color="#555555">030 </font>    String cmd = event.getActionCommand();
<font color="#555555">031 </font>    <font color="#006699">int</font> n1 = Integer.parseInt(tf.getText());
<font color="#555555">032 </font>    <font color="#006699">int</font> n2 = Integer.parseInt(cmd.substring(1));
<font color="#555555">033 </font>    tf.setText(<font color="#0000FF">""</font> + (n1 + n2));
<font color="#555555">034 </font>  }
<font color="#555555">035 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/CalculatorApplet2.java"><font color="#000055" size=-1>CalculatorApplet2.java</font></a></td>
</tr>
</table>
<i>
Listing 40.8: Das gleichwertige Calculator-Applet</i></p>

<p>
Nun kann die HTML-Datei erstellt und das Applet aufgerufen werden:
<a name="listingid040009"></a>

<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">&lt;html&gt;</font>
<font color="#555555">002 </font><font color="#0000AA">&lt;head&gt;</font>
<font color="#555555">003 </font><font color="#0000AA">&lt;title&gt;</font>CalculatorApplet2<font color="#0000AA">&lt;/title&gt;</font>
<font color="#555555">004 </font><font color="#0000AA">&lt;/head&gt;</font>
<font color="#555555">005 </font><font color="#0000AA">&lt;body&gt;</font>
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">&lt;h1&gt;</font>CalculatorApplet2<font color="#0000AA">&lt;/h1&gt;</font>
<font color="#555555">008 </font>
<font color="#555555">009 </font><font color="#0000AA">&lt;applet code=</font><font color="#0000FF">"CalculatorApplet2.class"</font><font color="#0000AA"> width=200 height=85&gt;</font>
<font color="#555555">010 </font>CalculatorApplet2
<font color="#555555">011 </font><font color="#0000AA">&lt;/applet&gt;</font>
<font color="#555555">012 </font>
<font color="#555555">013 </font><font color="#0000AA">&lt;/body&gt;</font>
<font color="#555555">014 </font><font color="#0000AA">&lt;/html&gt;</font></pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/CalculatorApplet2.html"><font color="#000055" size=-1>CalculatorApplet2.html</font></a></td>
</tr>
</table>
<i>
Listing 40.9: HTML-Datei zum Aufruf des Beispiel-Applets</i></p>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html">&nbsp;Titel&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html">&nbsp;Inhalt&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html">&nbsp;Suchen&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html">&nbsp;Index&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()">&nbsp;DOC&nbsp;</a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100254.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100256.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100258.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100260.html">&nbsp;&gt;&gt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()">&nbsp;API&nbsp;</a>
<td align="right">&copy; 1998, 2007 Guido Kr&uuml;ger &amp; Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>