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
|
<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,k100184.html;106,k100185.html;107,k100187.html;108,k100192.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"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100184.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100185.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100187.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100192.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 29 - Low-Level-Events
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id029002"></a>
<h2>29.2 <a name="ixa101955">Component-Events</a></h2>
<hr>
<ul>
<li><a href="k100186.html#sectlevel2id029002">29.2 Component-Events</a>
</ul>
<hr>
<p>
Ein Component-Event wird generiert, wenn eine Komponente verschoben
oder ihre Größe verändert wurde oder wenn sich der
Anzeigezustand einer Komponente verändert hat. Da sowohl Fenster
als auch alle Dialogelemente aus der Klasse <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>
abgeleitet sind, haben die hier erwähnten Ereignisse für
nahezu alle GUI-Elemente des AWT Gültigkeit.
<p>
Ein Empfänger für Component-Events muss das Interface <a name="ixa101956"><a href="index_c.html#ixb101448"><font color=#000080><tt>ComponentListener</tt></font></a></a>
implementieren und bekommt Events des Typs <a name="ixa101957"><a href="index_c.html#ixb101403"><font color=#000080><tt>ComponentEvent</tt></font></a></a>
übergeben. <a href="index_c.html#ixb101403"><font color=#000080><tt>ComponentEvent</tt></font></a>
erweitert die Klasse <a href="index_a.html#ixb101402"><font color=#000080><tt>AWTEvent</tt></font></a>
und stellt neben <a href="index_g.html#ixb101409"><font color=#000080><tt>getID</tt></font></a>
und <a href="index_g.html#ixb101401"><font color=#000080><tt>getSource</tt></font></a>
die Methode <a name="ixa101958"><a href="index_g.html#ixb101508"><font color=#000080><tt>getComponent</tt></font></a></a>
zur Verfügung, mit der die Komponente ermittelt werden kann,
die die Nachricht ausgelöst hat.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Component getComponent()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/event/ComponentEvent.html" onClick="this.href=getApiDoc('java.awt.event.ComponentEvent')"><font color="#660066" size=-1>java.awt.event.ComponentEvent</font></a></td>
</tr>
</table>
<p>
Die Registrierung der Empfängerklasse erfolgt mit der Methode
<a name="ixa101959"><a href="index_a.html#ixb101449"><font color=#000080><tt>addComponentListener</tt></font></a></a>,
die in allen aus <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>
abgeleiteten Klassen zur Verfügung steht:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void addComponentListener(ComponentListener l)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Component.html" onClick="this.href=getApiDoc('java.awt.Component')"><font color="#660066" size=-1>java.awt.Component</font></a></td>
</tr>
</table>
<p>
<a href="k100186.html#methodencomponentlistener">Tabelle 29.2</a>
gibt eine Übersicht der Methoden von <a href="index_c.html#ixb101448"><font color=#000080><tt>ComponentListener</tt></font></a>
und erklärt ihre Bedeutung: <a name="methodencomponentlistener"></a>
<p>
<table cols=2 border width=100%>
<tr>
<td valign=top align=left width=25%><b>Ereignismethode</b></td>
<td valign=top align=left width=75%><b>Bedeutung </b></td></tr>
<tr>
<td valign=top align=left><a name="ixa101960"><a href="index_c.html#ixb101453"><font color=#000080><tt>componentShown</tt></font></a></a></td>
<td valign=top align=left>Eine Komponente wurde sichtbar. </td></tr>
<tr>
<td valign=top align=left><a name="ixa101961"><a href="index_c.html#ixb101450"><font color=#000080><tt>componentHidden</tt></font></a></a></td>
<td valign=top align=left>Eine Komponente wurde unsichtbar. </td></tr>
<tr>
<td valign=top align=left><a name="ixa101962"><a href="index_c.html#ixb101451"><font color=#000080><tt>componentMoved</tt></font></a></a></td>
<td valign=top align=left>Eine Komponente wurde verschoben. </td></tr>
<tr>
<td valign=top align=left><a name="ixa101963"><a href="index_c.html#ixb101452"><font color=#000080><tt>componentResized</tt></font></a></a></td>
<td valign=top align=left>Die Größe einer Komponente hat
sich geändert. </td></tr>
</table>
<p><i>
Tabelle 29.2: Methoden von ComponentListener </i></p>
<p>
Das folgende Programm demonstriert die Anwendung der Methoden <a href="index_c.html#ixb101451"><font color=#000080><tt>componentMoved</tt></font></a>
und <a href="index_c.html#ixb101452"><font color=#000080><tt>componentResized</tt></font></a>
am Beispiel eines Fensters <font color="#000077"><tt>BirdsEyeFrame</tt></font>,
das schematisch sich selbst und seine Lage auf dem Desktop aus der
Vogelperspektive darstellt. Wird das Fenster verschoben oder seine
Größe geändert, so passt es seine eigene Darstellung
proportional an und zeichnet die Client-Area neu. Die Implementierung
der <a href="index_p.html#ixb101148"><font color=#000080><tt>paint</tt></font></a>-Methode
ermittelt dazu die Seitenverhältnisse von Fenster und Desktop
und verwendet diese als Quotient zur Anzeige des Fensters im Fenster.
<p>
Um auf die Component-Events zu reagieren, registriert <font color="#000077"><tt>BirdsEyeFrame</tt></font>
die Adapterklasse <font color="#000077"><tt>ComponentRepaintAdapter</tt></font>,
die die Methoden <a href="index_c.html#ixb101451"><font color=#000080><tt>componentMoved</tt></font></a>
und <a href="index_c.html#ixb101452"><font color=#000080><tt>componentResized</tt></font></a>
implementiert. Sie werden immer dann aufgerufen, wenn das Fenster
verschoben oder in der Größe geändert wurde, und rufen
<a name="ixa101964"><a href="index_r.html#ixb101509"><font color=#000080><tt>repaint</tt></font></a></a>
auf, um das Fenster neu zu zeichnen. Auf diese Weise werden alle Änderungen
des Frames sofort in seiner eigenen Client-Area gespiegelt:
<a name="listingid029002"></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">/* Listing2902.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">class</font> ComponentRepaintAdapter
<font color="#555555">007 </font><font color="#0000AA">extends</font> ComponentAdapter
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> componentMoved(ComponentEvent event)
<font color="#555555">010 </font> {
<font color="#555555">011 </font> event.getComponent().repaint();
<font color="#555555">012 </font> }
<font color="#555555">013 </font>
<font color="#555555">014 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> componentResized(ComponentEvent event)
<font color="#555555">015 </font> {
<font color="#555555">016 </font> event.getComponent().repaint();
<font color="#555555">017 </font> }
<font color="#555555">018 </font>}
<font color="#555555">019 </font>
<font color="#555555">020 </font><font color="#0000AA">class</font> BirdsEyeFrame
<font color="#555555">021 </font><font color="#0000AA">extends</font> Frame
<font color="#555555">022 </font>{
<font color="#555555">023 </font> <font color="#0000AA">public</font> BirdsEyeFrame()
<font color="#555555">024 </font> {
<font color="#555555">025 </font> <font color="#006699">super</font>(<font color="#0000FF">"BirdsEyeFrame"</font>);
<font color="#555555">026 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">027 </font> addComponentListener(<font color="#0000AA">new</font> ComponentRepaintAdapter());
<font color="#555555">028 </font> setBackground(Color.lightGray);
<font color="#555555">029 </font> }
<font color="#555555">030 </font>
<font color="#555555">031 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">032 </font> {
<font color="#555555">033 </font> Dimension screensize = getToolkit().getScreenSize();
<font color="#555555">034 </font> Dimension framesize = getSize();
<font color="#555555">035 </font> <font color="#006699">double</font> qx = framesize.width / (<font color="#006699">double</font>)screensize.width;
<font color="#555555">036 </font> <font color="#006699">double</font> qy = framesize.height / (<font color="#006699">double</font>)screensize.height;
<font color="#555555">037 </font> g.setColor(Color.white);
<font color="#555555">038 </font> g.fillRect(
<font color="#555555">039 </font> (<font color="#006699">int</font>)(qx * getLocation().x),
<font color="#555555">040 </font> (<font color="#006699">int</font>)(qy * getLocation().y),
<font color="#555555">041 </font> (<font color="#006699">int</font>)(qx * framesize.width),
<font color="#555555">042 </font> (<font color="#006699">int</font>)(qy * framesize.height)
<font color="#555555">043 </font> );
<font color="#555555">044 </font> g.setColor(Color.darkGray);
<font color="#555555">045 </font> g.fillRect(
<font color="#555555">046 </font> (<font color="#006699">int</font>)(qx * getLocation().x),
<font color="#555555">047 </font> (<font color="#006699">int</font>)(qy * getLocation().y),
<font color="#555555">048 </font> (<font color="#006699">int</font>)(qx * framesize.width),
<font color="#555555">049 </font> (<font color="#006699">int</font>)(qy * getInsets().top)
<font color="#555555">050 </font> );
<font color="#555555">051 </font> g.drawRect(
<font color="#555555">052 </font> (<font color="#006699">int</font>)(qx * getLocation().x),
<font color="#555555">053 </font> (<font color="#006699">int</font>)(qy * getLocation().y),
<font color="#555555">054 </font> (<font color="#006699">int</font>)(qx * framesize.width),
<font color="#555555">055 </font> (<font color="#006699">int</font>)(qy * framesize.height)
<font color="#555555">056 </font> );
<font color="#555555">057 </font> }
<font color="#555555">058 </font>}
<font color="#555555">059 </font>
<font color="#555555">060 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing2902
<font color="#555555">061 </font>{
<font color="#555555">062 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">063 </font> {
<font color="#555555">064 </font> BirdsEyeFrame wnd = <font color="#0000AA">new</font> BirdsEyeFrame();
<font color="#555555">065 </font> wnd.setSize(300,200);
<font color="#555555">066 </font> wnd.setLocation(200,100);
<font color="#555555">067 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">068 </font> }
<font color="#555555">069 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2902.java"><font color="#000055" size=-1>Listing2902.java</font></a></td>
</tr>
</table>
<i>
Listing 29.2: Das eigene Fenster aus der Vogelperspektive</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid029001"></a>
<img src="images/Vogelperspektive.gif">
<p>
<p><i>
Abbildung 29.1: Das Fenster sieht sich selbst aus der Vogelperspektive</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ßen des Fensters zu registrieren.
Damit ein solches Beispiel sich kompilieren läß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"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </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="k100184.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100185.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100187.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100192.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|