summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/html/k100176.html
blob: 35558c3a5bafbb7746433a92c0a6f96b4b72ba81 (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
<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,k100173.html;106,k100175.html;107,k100177.html;108,k100180.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="k100173.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100175.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100177.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100180.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 27 - Fenster
</table>
<hr>


<!-- Section -->
<a name="sectlevel2id027003"></a>
<h2>27.3 Visuelle Eigenschaften </h2>
<hr>
<ul>
<li><a href="k100176.html#sectlevel2id027003">27.3 Visuelle Eigenschaften</a>
</ul>
<hr>

<p>
Ein Fenster besitzt eine Reihe von Eigenschaften, die sein Aussehen
bestimmen. Dazu geh&ouml;ren die Art des Rahmens, die Position und
Gr&ouml;&szlig;e des Fensters und die Anordnung des Fensters in Relation
zu anderen Fenstern auf dem Bildschirm. 

<p>
Wie schon erw&auml;hnt, wird die Art des Rahmens durch die Klasse
bestimmt, die zur Erzeugung eines Fensters verwendet wird. W&auml;hrend
die Klasse <a href="index_w.html#ixb101146"><font color=#000080><tt>Window</tt></font></a>
ein Fenster ohne Rahmen darstellt, besitzt ein <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
einen Rahmen, eine Titelleiste und auf Wunsch ein Men&uuml;. 

<p>
Die Gr&ouml;&szlig;e und Position eines Fensters k&ouml;nnen mit den
Methoden <a name="ixa101761"><a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a></a>,
<a name="ixa101762"><a href="index_s.html#ixb101356"><font color=#000080><tt>setBounds</tt></font></a></a>
und <a name="ixa101763"><a href="index_s.html#ixb101357"><font color=#000080><tt>setLocation</tt></font></a></a>
bestimmt werden: 
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setSize(int width, int height)
public void setSize(Dimension d)

public void setBounds(int x, int y, int width, int height)
public void setBounds(Rectangle r)

public void setLocation(int x, int y)
public void setLocation(Point p)

public Dimension getSize()
public Rectangle getBounds()
public Point getLocation()
</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="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a>
ver&auml;ndert die Gr&ouml;&szlig;e des Fensters auf den Wert (<font color="#000077"><tt>width</tt></font>,
<font color="#000077"><tt>height</tt></font>), und <a href="index_s.html#ixb101357"><font color=#000080><tt>setLocation</tt></font></a>
bewegt die linke obere Ecke an die Bildschirmposition <font color="#000077"><tt>(x,y)</tt></font>.
Die Methode <a href="index_s.html#ixb101356"><font color=#000080><tt>setBounds</tt></font></a>
kombiniert die Funktionen von <a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a>
und <a href="index_s.html#ixb101357"><font color=#000080><tt>setLocation</tt></font></a>
und positioniert ein Fenster der Gr&ouml;&szlig;e <font color="#000077"><tt>(width,height)</tt></font>
an der Position <font color="#000077"><tt>(x,y)</tt></font>. Mit <a name="ixa101764"><a href="index_g.html#ixb101196"><font color=#000080><tt>getSize</tt></font></a></a>,
<a name="ixa101765"><a href="index_g.html#ixb101190"><font color=#000080><tt>getBounds</tt></font></a></a>
und <a name="ixa101766"><a href="index_g.html#ixb101358"><font color=#000080><tt>getLocation</tt></font></a></a>
k&ouml;nnen diese Eigenschaften auch abgefragt werden. 

<p>
Mit den Methoden <a name="ixa101767"><a href="index_s.html#ixb101359"><font color=#000080><tt>setEnabled</tt></font></a></a>
und <a name="ixa101768"><a href="index_i.html#ixb101360"><font color=#000080><tt>isEnabled</tt></font></a></a>
kann auf den Aktivierungszustand einer Komponente zugegriffen werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setEnabled(boolean b)
public boolean isEnabled()
</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>
Ist die Komponente aktiv, kann sie Benutzereingaben empfangen und
Nachrichten versenden. In deaktiviertem Zustand geht das nicht. Dialogelemente
wie Textfelder oder Buttons werden deaktiviert, um dem Anwender anzuzeigen,
dass eine bestimmte Funktion oder Eingabem&ouml;glichkeit derzeit
nicht zur Verf&uuml;gung steht. Sie zeigen den Unterschied zwischen
beiden Zust&auml;nden auch optisch an. Bei Fensterkomponenten wird
der Aktivierungszustand meist nicht ver&auml;ndert. 

<p>
Das folgende Beispiel stellt eine sehr einfache Form eines (manuell
zu aktivierenden) &#187;Bildschirmschoners&#171; dar, der den gesamten
Bildschirm dunkel schaltet und die Anmerkung &#187;Bitte eine Taste
dr&uuml;cken&#171; in die linke obere Ecke schreibt. Zus&auml;tzlich
gibt das Programm die aktuelle Aufl&ouml;sung des Bildschirms aus,
nachdem sie mit der Methode <a name="ixa101769"><a href="index_g.html#ixb101361"><font color=#000080><tt>getScreenSize</tt></font></a></a>
der Klasse <a href="index_t.html#ixb100506"><font color=#000080><tt>Toolkit</tt></font></a>
ermittelt wurde. Nach Dr&uuml;cken einer Taste wird das Fenster geschlossen
und das Programm beendet: 
<a name="bildschirmschoner"></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">/* Listing2702.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> Listing2702
<font color="#555555">007 </font><font color="#0000AA">extends</font> Window
<font color="#555555">008 </font>{
<font color="#555555">009 </font>  <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">010 </font>  {
<font color="#555555">011 </font>    <font color="#0000AA">final</font> Listing2702 wnd = <font color="#0000AA">new</font> Listing2702();
<font color="#555555">012 </font>    wnd.setLocation(<font color="#0000AA">new</font> Point(0,0));
<font color="#555555">013 </font>    wnd.setSize(wnd.getToolkit().getScreenSize());
<font color="#555555">014 </font>    wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">015 </font>    wnd.requestFocus();
<font color="#555555">016 </font>    wnd.addKeyListener(
<font color="#555555">017 </font>      <font color="#0000AA">new</font> KeyAdapter() {
<font color="#555555">018 </font>        <font color="#0000AA">public</font> <font color="#006699">void</font> keyPressed(KeyEvent event)
<font color="#555555">019 </font>        {
<font color="#555555">020 </font>          wnd.setVisible(<font color="#006699">false</font>);
<font color="#555555">021 </font>          wnd.dispose();
<font color="#555555">022 </font>          System.exit(0);
<font color="#555555">023 </font>        }
<font color="#555555">024 </font>      }
<font color="#555555">025 </font>    );
<font color="#555555">026 </font>  }
<font color="#555555">027 </font>
<font color="#555555">028 </font>  <font color="#0000AA">public</font> Listing2702()
<font color="#555555">029 </font>  {
<font color="#555555">030 </font>    <font color="#006699">super</font>(<font color="#0000AA">new</font> Frame());
<font color="#555555">031 </font>    setBackground(Color.black);
<font color="#555555">032 </font>  }
<font color="#555555">033 </font>
<font color="#555555">034 </font>  <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">035 </font>  {
<font color="#555555">036 </font>    g.setColor(Color.red);
<font color="#555555">037 </font>    g.drawString(
<font color="#555555">038 </font>      <font color="#0000FF">"Bildschirmgr&ouml;&szlig;e ist "</font>+
<font color="#555555">039 </font>      getSize().width+<font color="#0000FF">"*"</font>+getSize().height,
<font color="#555555">040 </font>      10,
<font color="#555555">041 </font>      20
<font color="#555555">042 </font>    );
<font color="#555555">043 </font>    g.drawString(<font color="#0000FF">"Bitte eine Taste dr&uuml;cken"</font>,10,40);
<font color="#555555">044 </font>  }
<font color="#555555">045 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing2702.java"><font color="#000055" size=-1>Listing2702.java</font></a></td>
</tr>
</table>
<i>
Listing 27.2: Ein einfacher Bildschirmschoner</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>
Die Details des hier implementierten Event-Handlings mit Hilfe der
durch <a name="ixa101770"><a href="index_a.html#ixb101362"><font color=#000080><tt>addKeyListener</tt></font></a></a>
eingef&uuml;gten anonymen Klasse wollen wir auf <a href="k100180.html#kapiteleventhandling">Kapitel 28</a>
vertagen. Die Ausgabe des Programms (in verkleinerter Form) ist:</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>
<p>
<a name="imageid027002"></a>
<img src="images/BildSchoner.gif">
<p>

<p><i>
Abbildung 27.2: Ein einfacher Bildschirmschoner</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="k100173.html">&nbsp;&lt;&lt;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100175.html">&nbsp;&nbsp;&lt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100177.html">&nbsp;&nbsp;&gt;&nbsp;&nbsp;</a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100180.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>