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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
|
<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,k100201.html;106,k100202.html;107,k100204.html;108,k100206.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="k100201.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100202.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100204.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100206.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 31 - GUI-Dialoge
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id031002"></a>
<h2>31.2 Die <a name="ixa102107">Layoutmanager</a> </h2>
<hr>
<ul>
<li><a href="k100203.html#sectlevel2id031002">31.2 Die Layoutmanager</a>
<ul>
<li><a href="k100203.html#sectlevel3id031002001">31.2.1 FlowLayout</a>
<li><a href="k100203.html#sectlevel3id031002002">31.2.2 GridLayout</a>
<ul>
<li><a href="k100203.html#sectlevel4id031002002001">Die Größe der Komponenten</a>
</ul>
<li><a href="k100203.html#sectlevel3id031002003">31.2.3 BorderLayout</a>
<li><a href="k100203.html#sectlevel3id031002004">31.2.4 GridBagLayout</a>
<li><a href="k100203.html#sectlevel3id031002005">31.2.5 NULL-Layout</a>
<li><a href="k100203.html#sectlevel3id031002006">31.2.6 Schachteln von Layoutmanagern </a>
</ul>
</ul>
<hr>
<p>
In vielen grafischen Oberflächen wird die Anordnung der Elemente
eines Dialoges durch Angabe <i>absoluter Koordinaten</i> vorgenommen.
Dabei wird für jede Komponente manuell oder mit Hilfe eines Ressourcen-Editors
pixelgenau festgelegt, an welcher Stelle im Dialog sie zu erscheinen
hat.
<p>
Da Java-Programme auf vielen unterschiedlichen Plattformen mit unterschiedlichen
Ausgabegeräten laufen sollen, war eine solche Vorgehensweise
für die Designer des AWT nicht akzeptabel. Sie wählten statt
dessen den Umweg über einen Layoutmanager, der für die Anordnung
der Dialogelemente verantwortlich ist. Um einen Layoutmanager verwenden
zu können, wird dieser dem Fenster vor der Übergabe der
Dialogelemente mit der Methode <a name="ixa102108"><a href="index_s.html#ixb101597"><font color=#000080><tt>setLayout</tt></font></a></a>
zugeordnet. Er ordnet dann die per <a name="ixa102109"><a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a></a>
übergebenen Elemente auf dem Fenster an.
<p>
Jeder Layoutmanager implementiert seine eigene Logik bezüglich
der optimalen Anordnung der Komponenten:
<ul>
<li>Das <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
ordnet Dialogelemente nebeneinander in einer Zeile an. Wenn keine
weiteren Elemente in die Zeile passen, wird mit der nächsten
Zeile fortgefahren.
<li>Das <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
ordnet die Dialogelemente in einem rechteckigen Gitter an, dessen
Zeilen- und Spaltenzahl beim Erstellen des Layoutmanagers angegeben
wird.
<li>Das <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
verteilt die Dialogelemente nach Vorgabe des Programms auf die vier
Randbereiche und den Mittelbereich des Fensters.
<li>Das <a href="index_c.html#ixb101601"><font color=#000080><tt>CardLayout</tt></font></a>
ist in der Lage, mehrere Unterdialoge in einem Fenster unterzubringen
und jeweils einen davon auf Anforderung des Programms anzuzeigen.
<li>Das <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
ist ein komplexer Layoutmanager, der die Fähigkeiten von <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
erweitert und es ermöglicht, mit Hilfe von Bedingungsobjekten
sehr komplexe Layouts zu erzeugen.
</ul>
<p>
Neben den gestalterischen Fähigkeiten eines Layoutmanagers bestimmt
in der Regel die Reihenfolge der Aufrufe der <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>-Methode
des Fensters die tatsächliche Anordnung der Komponenten auf dem
Bildschirm. Wenn nicht - wie es z.B. beim <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
möglich ist - zusätzliche Positionierungsinformationen an
das Fenster übergeben werden, ordnet der jeweilige Layoutmanager
die Komponenten in der Reihenfolge ihres Eintreffens an.
<p>
Um komplexere Layouts realisieren zu können, als die Layoutmanager
sie in ihren jeweiligen Grundausprägungen bieten, gibt es die
Möglichkeit, Layoutmanager zu schachteln. Auf diese Weise kann
auch ohne Vorgabe fester Koordinaten fast jede gewünschte Komponentenanordnung
realisiert werden. Sollte auch diese Variante nicht genau genug sein,
so bietet sich schließlich durch Verwendung eines <a name="ixa102110"><i>Null-Layouts</i></a>
die Möglichkeit an, Komponenten durch Vorgabe fester Koordinaten
zu platzieren.
<!-- Section -->
<a name="sectlevel3id031002001"></a>
<h3>31.2.1 FlowLayout </h3>
<p>
Die Klasse <a name="ixa102111"><a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a></a>
stellt den einfachsten Layoutmanager dar. Alle Elemente werden so
lange nacheinander in einer Zeile angeordnet, bis kein Platz mehr
vorhanden ist und in der nächsten Zeile fortgefahren wird.
<p>
Das <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
wird einem Fenster durch folgende Anweisung zugeordnet:
<font color="#000077">
<pre>
setLayout(new FlowLayout());
</pre>
</font>
<p>
Neben dem parameterlosen gibt es weitere Konstruktoren, die die Möglichkeit
bieten, die Anordnung der Elemente und ihre Abstände voneinander
vorzugeben:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public FlowLayout(int align)
public FlowLayout(int align, int hgap, int vgap)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/FlowLayout.html" onClick="this.href=getApiDoc('java.awt.FlowLayout')"><font color="#660066" size=-1>java.awt.FlowLayout</font></a></td>
</tr>
</table>
<p>
Der Parameter <font color="#000077"><tt>align</tt></font> gibt an,
ob die Elemente einer Zeile linksbündig, rechtsbündig oder
zentriert angeordnet werden. Hierzu stehen die Konstanten <a name="ixa102112"><a href="index_c.html#ixb101608"><font color=#000080><tt>FlowLayout.CENTER</tt></font></a></a>,
<a name="ixa102113"><a href="index_l.html#ixb101614"><font color=#000080><tt>FlowLayout.LEFT</tt></font></a></a>
und <a name="ixa102114"><a href="index_r.html#ixb101615"><font color=#000080><tt>FlowLayout.RIGHT</tt></font></a></a>
zur Verfügung, als Voreinstellung wird <a href="index_c.html#ixb101608"><font color=#000080><tt>FlowLayout.CENTER</tt></font></a>
verwendet. Mit <font color="#000077"><tt>hgap</tt></font> und <font color="#000077"><tt>vgap</tt></font>
können die horizontalen und vertikalen Abstände zwischen
den Komponenten vorgegeben werden. Die Voreinstellung ist 5.
<p>
Das folgende Listing zeigt die Verwendung der Klasse <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
am Beispiel von fünf Buttons, die in einem Grafikfenster angezeigt
werden. Anstelle der normalen zentrierten Anordnung werden die Elemente
linksbündig angeordnet, und der Abstand zwischen den Buttons
beträgt 20 Pixel:
<a name="flowlayoutbsp"></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">/* Listing3102.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> Listing3102
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3102 wnd = <font color="#0000AA">new</font> Listing3102();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3102()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Test FlowLayout"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> setLayout(<font color="#0000AA">new</font> FlowLayout(FlowLayout.LEFT,20,20));
<font color="#555555">021 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 1"</font>));
<font color="#555555">022 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 2"</font>));
<font color="#555555">023 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 3"</font>));
<font color="#555555">024 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 4"</font>));
<font color="#555555">025 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 5"</font>));
<font color="#555555">026 </font> pack();
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3102.java"><font color="#000055" size=-1>Listing3102.java</font></a></td>
</tr>
</table>
<i>
Listing 31.2: Die Klasse FlowLayout</i></p>
<p>
Das Programm erzeugt folgendes Fenster:
<p>
<a name="imageid031002"></a>
<img src="images/FlowLayout.gif">
<p>
<p><i>
Abbildung 31.2: Verwendung der Klasse FlowLayout</i></p>
<!-- Section -->
<a name="sectlevel3id031002002"></a>
<h3>31.2.2 GridLayout </h3>
<p>
Ein <a name="ixa102115"><a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a></a>
bietet eine größere Kontrolle über die Anordnung der
Elemente als ein <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>.
Hier werden die Komponenten nicht einfach nacheinander auf dem Bildschirm
positioniert, sondern innerhalb eines rechteckigen Gitters angeordnet,
dessen Elemente eine feste Größe haben.
<p>
Das Programm übergibt dazu beim Aufruf des Konstruktors zwei
Parameter, <font color="#000077"><tt>rows</tt></font> und <font color="#000077"><tt>columns</tt></font>,
mit denen die vertikale und horizontale Anzahl an Elementen festgelegt
wird:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public GridLayout(int rows, int columns)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/GridLayout.html" onClick="this.href=getApiDoc('java.awt.GridLayout')"><font color="#660066" size=-1>java.awt.GridLayout</font></a></td>
</tr>
</table>
<p>
Beim Aufruf von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
werden die Komponenten dann nacheinander in die einzelnen Zellen der
Gittermatrix gelegt. Analog zum <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
wird dabei zunächst die erste Zeile von links nach rechts gefüllt,
dann die zweite usw.
<p>
Ähnlich wie beim <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
steht ein zusätzlicher Konstruktor zur Verfügung, der es
erlaubt, die Größe der horizontalen und vertikalen Lücken
zu verändern:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public GridLayout(int rows, int columns, int hgap, int vgap)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/GridLayout.html" onClick="this.href=getApiDoc('java.awt.GridLayout')"><font color="#660066" size=-1>java.awt.GridLayout</font></a></td>
</tr>
</table>
<p>
Die größere Kontrolle des Programms über das Layout
besteht nun darin, dass der Umbruch in die nächste Zeile genau
vorhergesagt werden kann. Anders als beim <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
erfolgt dieser nicht erst dann, wenn keine weiteren Elemente in die
Zeile passen, sondern wenn dort so viele Elemente platziert wurden,
wie das Programm vorgegeben hat.
<p>
Das folgende Beispiel zeigt ein Fenster mit einem Gitter der Größe
4*2 Elemente, das ingesamt sieben Buttons anzeigt:
<a name="gridlayoutbsp"></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">/* Listing3103.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> Listing3103
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3103 wnd = <font color="#0000AA">new</font> Listing3103();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3103()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Test GridLayout"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> setLayout(<font color="#0000AA">new</font> GridLayout(4,2));
<font color="#555555">021 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 1"</font>));
<font color="#555555">022 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 2"</font>));
<font color="#555555">023 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 3"</font>));
<font color="#555555">024 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 4"</font>));
<font color="#555555">025 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 5"</font>));
<font color="#555555">026 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 6"</font>));
<font color="#555555">027 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 7"</font>));
<font color="#555555">028 </font> pack();
<font color="#555555">029 </font> }
<font color="#555555">030 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3103.java"><font color="#000055" size=-1>Listing3103.java</font></a></td>
</tr>
</table>
<i>
Listing 31.3: Die Klasse GridLayout</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid031003"></a>
<img src="images/GridLayout.gif">
<p>
<p><i>
Abbildung 31.3: Verwendung der Klasse GridLayout</i></p>
<!-- Section -->
<a name="sectlevel4id031002002001"></a>
<h4>Die Größe der Komponenten </h4>
<p>
Wenn wir die Größe des Fensters nicht durch einen Aufruf
von <a href="index_p.html#ixb101611"><font color=#000080><tt>pack</tt></font></a>,
sondern manuell festgelegt hätten, wäre an dieser Stelle
ein wichtiger Unterschied zwischen den beiden bisher vorgestellten
Layoutmanagern deutlich geworden. <a href="k100203.html#flowlayoutgross">Abbildung 31.4</a>
zeigt das veränderte <a href="k100203.html#flowlayoutbsp">Listing 31.2</a>,
bei dem die Größe des Fensters durch Aufruf von <font color="#000077"><tt>setSize(500,200);</tt></font>
auf 500*200 Pixel festgelegt und der Aufruf von <a href="index_p.html#ixb101611"><font color=#000080><tt>pack</tt></font></a>
entfernt wurde:
<p>
<a name="flowlayoutgross"></a>
<img src="images/FlowLayoutGross.gif">
<p>
<p><i>
Abbildung 31.4: Das FlowLayout in einem größeren Fenster</i></p>
<p>
Hier verhält sich das Programm noch so, wie wir es erwarten würden,
denn die Buttons haben ihre Größe behalten, während
das Fenster größer geworden ist. Anders sieht es dagegen
aus, wenn wir die Fenstergröße in <a href="k100203.html#gridlayoutbsp">Listing 31.3</a>
ebenfalls auf 500*200 Pixel fixieren:
<p>
<a name="imageid031005"></a>
<img src="images/GridLayoutGross.gif">
<p>
<p><i>
Abbildung 31.5: Das GridLayout in einem größeren Fenster</i></p>
<p>
Nun werden die Buttons plötzlich sehr viel größer
als ursprünglich angezeigt, obwohl sie eigentlich weniger Platz
in Anspruch nehmen würden. Der Unterschied besteht darin, dass
ein <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
die <i>gewünschte</i> Größe eines Dialogelements verwendet,
um seine Ausmaße zu bestimmen. Das <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
dagegen ignoriert die gewünschte Größe und dimensioniert
die Dialogelemente fest in der Größe eines Gitterelements.
Ein <font color="#000077"><tt>LayoutManager</tt></font> hat also offensichtlich
die Freiheit zu entscheiden, ob und in welcher Weise er die Größen
von Fenster und Dialogelementen den aktuellen Erfordernissen anpasst.
<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>
Wir werden am Ende dieses Abschnitts auch Möglichkeiten kennenlernen,
durch Schachteln von Layoutmanagern beide Möglichkeiten zu kombinieren:
Das feste Gitterraster bleibt erhalten, aber die einzelnen Komponenten
innerhalb des Gitters werden in ihrer gewünschten Größe
angezeigt. Am Ende von <a href="k100206.html#kapitelawtdialogelemente">Kapitel 32</a>
werden wir zusätzlich kurz auf die verschiedenen Größen
von Dialogelementen zurückkommen, wenn wir zeigen, wie selbstdefinierte
Komponenten erzeugt werden.</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>
<!-- Section -->
<a name="sectlevel3id031002003"></a>
<h3>31.2.3 BorderLayout </h3>
<p>
Das <a name="ixa102116"><a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a></a>
verfolgt einen anderen Ansatz als die beiden vorigen Layoutmanager,
denn die Positionierung der Komponenten wird nicht mehr primär
durch die Reihenfolge der Aufrufe von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
bestimmt. Statt dessen teilt das <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
den Bildschirm in fünf Bereiche auf, und zwar in die vier Ränder
und das Zentrum. Durch Angabe eines <a name="ixa102117"><i>Himmelsrichtungs-Constraints</i></a>
wird beim Aufruf von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
angegeben, auf welchem dieser Bereiche die Komponente platziert werden
soll. <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
stellt dazu folgende Konstanten zur Verfügung:
<ul>
<li>South: unterer Rand
<li>North: oberer Rand
<li>East: rechter Rand
<li>West: linker Rand
<li>Center: Mitte
</ul>
<p>
Zur Übergabe dieses Parameters gibt es die Methode <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
der Klasse <a href="index_c.html#ixb101353"><font color=#000080><tt>Container</tt></font></a>
in einer Variante mit einem <a href="index_o.html#ixb100224"><font color=#000080><tt>Object</tt></font></a>
als zweiten Parameter:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void add(Component comp, Object constraints)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Container.html" onClick="this.href=getApiDoc('java.awt.Container')"><font color="#660066" size=-1>java.awt.Container</font></a></td>
</tr>
</table>
<p>
Das folgende Beispiel zeigt die Anordnung von fünf Buttons in
einem <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>.
Jeweils einer der Buttons wird auf die vier Randbereiche verteilt,
und der fünfte Button steht in der Mitte. Die Größe
des Fensters wird fest auf 300*200 Pixel eingestellt:
<a name="listingid031004"></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">/* Listing3104.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> Listing3104
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3104 wnd = <font color="#0000AA">new</font> Listing3104();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3104()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Test BorderLayout"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> setSize(300,200);
<font color="#555555">021 </font> setLayout(<font color="#0000AA">new</font> BorderLayout());
<font color="#555555">022 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 1"</font>), BorderLayout.NORTH);
<font color="#555555">023 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 2"</font>), BorderLayout.SOUTH);
<font color="#555555">024 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 3"</font>), BorderLayout.WEST);
<font color="#555555">025 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 4"</font>), BorderLayout.EAST);
<font color="#555555">026 </font> add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button 5"</font>), BorderLayout.CENTER);
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3104.java"><font color="#000055" size=-1>Listing3104.java</font></a></td>
</tr>
</table>
<i>
Listing 31.4: Das BorderLayout</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid031006"></a>
<img src="images/BorderLayout.gif">
<p>
<p><i>
Abbildung 31.6: Verwendung der Klasse BorderLayout</i></p>
<p>
Bezüglich der Skalierung der Komponenten verfolgt <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
einen Mittelweg zwischen <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
und <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>.
Während <a href="index_f.html#ixb101598"><font color=#000080><tt>FlowLayout</tt></font></a>
die Komponenten immer in ihrer gewünschten Größe beläßt
und <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
sie immer skaliert, ist dies bei <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
von verschiedenen Faktoren abhängig:
<ul>
<li> Nord- und Südelement behalten ihre gewünschte Höhe,
werden aber auf die volle Fensterbreite skaliert.
<li> Ost- und Westelement behalten ihre gewünschte Breite, werden
aber in der Höhe so skaliert, dass sie genau zwischen Nord- und
Südelement passen.
<li> Das Mittelelement wird in der Höhe und Breite so angepasst,
dass es den verbleibenden freien Raum einnimmt.
</ul>
<p>
Auch beim <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
kann die Größe der Lücken zwischen den Elementen an
den Konstruktor übergeben werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public BorderLayout(int hgap, int vgap)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/BorderLayout.html" onClick="this.href=getApiDoc('java.awt.BorderLayout')"><font color="#660066" size=-1>java.awt.BorderLayout</font></a></td>
</tr>
</table>
<p>
Wenn das vorige Beispiel ein <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>
mit einer vertikalen und horizontalen Lücke von 10 Pixeln definieren
würde, wäre die Ausgabe des Programms wie folgt:
<p>
<a name="imageid031007"></a>
<img src="images/BorderLayoutLuecken.gif">
<p>
<p><i>
Abbildung 31.7: Ein BorderLayout mit Lücken</i></p>
<!-- Section -->
<a name="sectlevel3id031002004"></a>
<h3>31.2.4 GridBagLayout </h3>
<p>
Das <a name="ixa102118"><a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a></a>
ist der aufwändigste Layoutmanager in Java. Er erlaubt eine sehr
flexible Gestaltung der Oberfläche und bietet viele Möglichkeiten,
die in den anderen Layoutmanagern nicht zu finden sind. Der Preis
dafür ist eine etwas kompliziertere Bedienung und ein höherer
Einarbeitungsaufwand. Wir wollen in diesem Abschnitt die Klasse <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
vorstellen und ihre grundlegenden Anwendungsmöglichkeiten besprechen.
<p>
Um mit einem <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
zu arbeiten, ist wie folgt vorzugehen:
<ul>
<li>Zunächst ist ein Objekt des Typs <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
zu instanzieren und durch Aufruf von <a href="index_s.html#ixb101597"><font color=#000080><tt>setLayout</tt></font></a>
dem Fenster zuzuweisen.
<li>Für jedes einzufügende Dialogelement ist nun ein Objekt
des Typs <a name="ixa102119"><a href="index_g.html#ixb101617"><font color=#000080><tt>GridBagConstraints</tt></font></a></a>
anzulegen, um über dessen Membervariablen die spezifischen Layouteigenschaften
des Dialogelements zu beschreiben.
<li>Sowohl das Dialogelement als auch das Eigenschaftenobjekt werden
durch Aufruf der Methode <a name="ixa102120"><a href="index_s.html#ixb101618"><font color=#000080><tt>setConstraints</tt></font></a></a>
an den Layoutmanager übergeben.
<li>Schließlich wird das Dialogelement mit <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
an das Fenster übergeben.
</ul>
<p>
Das könnte beispielsweise so aussehen
<a name="listingid031005"></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="#555555">002 </font>GridBagLayout gbl = <font color="#0000AA">new</font> GridBagLayout();
<font color="#555555">003 </font>GridBagConstraints gbc = <font color="#0000AA">new</font> GridBagConstraints();
<font color="#555555">004 </font>setLayout(gbl);
<font color="#555555">005 </font>List list = <font color="#0000AA">new</font> List();
<font color="#555555">006 </font>gbc.gridx = 0;
<font color="#555555">007 </font>gbc.gridy = 0;
<font color="#555555">008 </font>gbc.gridwidth = 1;
<font color="#555555">009 </font>gbc.gridheight = 1;
<font color="#555555">010 </font>gbc.weightx = 100;
<font color="#555555">011 </font>gbc.weighty = 100;
<font color="#555555">012 </font>gbc.fill = GridBagConstraints.BOTH;
<font color="#555555">013 </font>gbl.setConstraints(list, gbc);
<font color="#555555">014 </font>add(list);
<font color="#555555">015 </font>...</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 31.5: Umgang mit GridBagLayout und GridBagConstraints</i></p>
<p>
Das Geheimnis der korrekten Verwendung eines <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
liegt also in der richtigen Konfiguration der Membervariablen der
<a href="index_g.html#ixb101617"><font color=#000080><tt>GridBagConstraints</tt></font></a>-Objekte.
Es stehen folgende zur Verfügung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public int gridx
public int gridy
public int gridwidth
public int gridheight
public int anchor
public int fill
public double weightx
public double weighty
public Insets insets
public int ipadx
public int ipady
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/GridBagConstraints.html" onClick="this.href=getApiDoc('java.awt.GridBagConstraints')"><font color="#660066" size=-1>java.awt.GridBagConstraints</font></a></td>
</tr>
</table>
<p>
Ihre Bedeutung ist:
<ul>
<li>Ein <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
unterteilt das Fenster ähnlich wie das <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
in ein rechteckiges Gitter von Zellen, in denen die Dialogelemente
platziert werden. Im Gegensatz zum <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
können die Zellen allerdings unterschiedliche Größen
haben, und die Dialogelemente können auf verschiedene Weise innerhalb
der Zellen platziert werden. Zudem kann ein Dialogelement sich in
beiden Richtungen über mehr als eine einzige Zelle erstrecken.
<li>Der Parameter <a name="ixa102121"><a href="index_g.html#ixb101619"><font color=#000080><tt>gridx</tt></font></a></a>
gibt an, in welcher Spalte des logischen Gitters der linke Rand des
Dialogelements liegen soll. Die erste Spalte hat den Wert 0. <a name="ixa102122"><a href="index_g.html#ixb101620"><font color=#000080><tt>gridy</tt></font></a></a>
gibt analog die logische Zeile des oberen Rands des Dialogelements
an. Beide Werte werden nicht etwa in Pixeln angegeben, sondern bezeichnen
die Zellen des logischen Gitternetzes. (0, 0) ist die linke obere
Ecke.
<li>Die Parameter <a name="ixa102123"><a href="index_g.html#ixb101621"><font color=#000080><tt>gridwidth</tt></font></a></a>
und <a name="ixa102124"><a href="index_g.html#ixb101622"><font color=#000080><tt>gridheight</tt></font></a></a>
geben die horizontale und vertikale Ausdehnung des Dialogelements
an. Soll ein Dialogelement beispielsweise zwei Zellen breit sein,
so ist für <a href="index_g.html#ixb101621"><font color=#000080><tt>gridwidth</tt></font></a>
der Wert 2 anzugeben.
<li>Mit dem Parameter <a name="ixa102125"><a href="index_f.html#ixb100869"><font color=#000080><tt>fill</tt></font></a></a>
wird definiert, wie sich die Abmessungen des Dialogelements verändern,
wenn die Größe des Fensters verändert wird. Wird hier
<a name="ixa102126"><a href="index_n.html#ixb101623"><font color=#000080><tt>GridBagConstraints.NONE</tt></font></a></a>
angegeben, bleibt das Element immer in seiner ursprünglichen
Größe. Wird einer der Werte <a name="ixa102127"><a href="index_h.html#ixb101624"><font color=#000080><tt>GridBagConstraints.HORIZONTAL</tt></font></a></a>,
<a name="ixa102128"><a href="index_v.html#ixb101625"><font color=#000080><tt>GridBagConstraints.VERTICAL</tt></font></a></a>
oder <a name="ixa102129"><a href="index_b.html#ixb101626"><font color=#000080><tt>GridBagConstraints.BOTH</tt></font></a></a>
angegeben, skaliert der Layoutmanager das Dialogelement automatisch
in horizontaler, vertikaler oder in beiden Richtungen.
<li>Der Parameter <a name="ixa102130"><a href="index_a.html#ixb101627"><font color=#000080><tt>anchor</tt></font></a></a>
bestimmt, an welcher Kante der Zelle das Dialogelement festgemacht
wird, wenn nach dem Skalieren noch Platz in der Zelle verbleibt. Hier
können die folgenden Konstanten angegeben werden:
<ul>
<li><a name="ixa102131"><a href="index_c.html#ixb101608"><font color=#000080><tt>GridBagConstraints.CENTER</tt></font></a></a>
<li><a name="ixa102132"><a href="index_n.html#ixb101605"><font color=#000080><tt>GridBagConstraints.NORTH</tt></font></a></a>
<li><a name="ixa102133"><a href="index_n.html#ixb101628"><font color=#000080><tt>GridBagConstraints.NORTHEAST</tt></font></a></a>
<li><a name="ixa102134"><a href="index_e.html#ixb101607"><font color=#000080><tt>GridBagConstraints.EAST</tt></font></a></a>
<li><a name="ixa102135"><a href="index_s.html#ixb101629"><font color=#000080><tt>GridBagConstraints.SOUTHEAST</tt></font></a></a>
<li><a name="ixa102136"><a href="index_s.html#ixb101604"><font color=#000080><tt>GridBagConstraints.SOUTH</tt></font></a></a>
<li><a name="ixa102137"><a href="index_s.html#ixb101630"><font color=#000080><tt>GridBagConstraints.SOUTHWEST</tt></font></a></a>
<li><a name="ixa102138"><a href="index_w.html#ixb101606"><font color=#000080><tt>GridBagConstraints.WEST</tt></font></a></a>
<li><a name="ixa102139"><a href="index_n.html#ixb101631"><font color=#000080><tt>GridBagConstraints.NORTHWEST</tt></font></a></a>
</ul>
<li>Die Parameter <a name="ixa102140"><a href="index_w.html#ixb101632"><font color=#000080><tt>weightx</tt></font></a></a>
und <a name="ixa102141"><a href="index_w.html#ixb101633"><font color=#000080><tt>weighty</tt></font></a></a>
sind etwas trickreich. Sie bestimmen das Verhältnis, in dem überschüssiger
Platz auf die Zellen einer Zeile bzw. einer Spalte verteilt wird.
Ist der Wert 0, bekommt die Zelle nichts von eventuell überschüssigem
Platz ab. Bedeutsam ist dabei nur das relative Verhältnis, der
absolute Wert dieses Parameters spielt keine Rolle. Die Werte sollten
allerdings nicht negativ sein.
<li>Mit dem Parameter <a name="ixa102142"><a href="index_i.html#ixb101634"><font color=#000080><tt>insets</tt></font></a></a>
kann ein Rand gesetzt werden. Er wird um das Dialogelement herum auf
jeden Fall freigelassen.
<li>Mit <a name="ixa102143"><a href="index_i.html#ixb101635"><font color=#000080><tt>ipadx</tt></font></a></a>
und <a name="ixa102144"><a href="index_i.html#ixb101636"><font color=#000080><tt>ipady</tt></font></a></a>
kann ein konstanter Wert zur minimalen Breite und Höhe der Komponente
hinzugefügt werden.
</ul>
<p>
Um die Anwendung der Parameter zu veranschaulichen, wollen wir uns
ein Beispiel ansehen. Das zu erstellende Programm soll ein Fenster
mit sechs Dialogelementen entsprechend <a href="k100203.html#gridbaglayout1">Abbildung 31.8</a>
aufbauen. Die Liste auf der linken Seite soll beim Skalieren in beide
Richtungen vergrößert werden. Die Textfelder sollen dagegen
lediglich in der Breite wachsen, ihre ursprüngliche Höhe
aber beibehalten. Bei vertikaler Vergrößerung des Fensters
sollen sie untereinander stehen bleiben. Die Beschriftungen sollen
ihre anfängliche Größe beibehalten und - unabhängig
von der Fenstergröße - direkt vor den Textfeldern stehen
bleiben. Auch der Button soll seine ursprüngliche Größe
beibehalten, wenn das Fenster skaliert wird. Zudem soll er immer in
der rechten unteren Ecke stehen. Zwischen den Dialogelementen soll
ein Standardabstand von zwei Pixeln eingehalten werden.
<p>
<a name="gridbaglayout1"></a>
<img src="images/GridBagLayoutBsp1.gif">
<p>
<p><i>
Abbildung 31.8: Beispiel für GridBagLayout</i></p>
<p>
Wir konstruieren das <a href="index_g.html#ixb101602"><font color=#000080><tt>GridBagLayout</tt></font></a>
als Zelle der Größe drei mal drei gemäß <a href="k100203.html#gridbaglayout2">Abbildung 31.9</a>:
<p>
<a name="gridbaglayout2"></a>
<img src="images/GridBagLayout.gif">
<p>
<p><i>
Abbildung 31.9: Zellenschema für GridBagLayout-Beispiel</i></p>
<p>
Die Listbox beginnt also in Zelle (0, 0) und erstreckt sich über
eine Zelle in horizontaler und drei Zellen in vertikaler Richtung.
Die beiden Labels liegen in Spalte 1 und den Zeilen 0 bzw. 1. Die
Textfelder liegen eine Spalte rechts daneben. Textfelder und Beschriftungen
sind genau eine Zelle breit und eine Zelle hoch. Der Button liegt
in Zelle (2, 2). Die übrigen Eigenschaften werden entsprechend
unserer Beschreibung so zugewiesen, dass die vorgegebenen Anforderungen
erfüllt werden:
<a name="listingid031006"></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">/* Listing3106.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> Listing3106
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3106 wnd = <font color="#0000AA">new</font> Listing3106();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3106()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Test GridBagLayout"</font>);
<font color="#555555">018 </font> setBackground(Color.lightGray);
<font color="#555555">019 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">020 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">021 </font> GridBagLayout gbl = <font color="#0000AA">new</font> GridBagLayout();
<font color="#555555">022 </font> GridBagConstraints gbc;
<font color="#555555">023 </font> setLayout(gbl);
<font color="#555555">024 </font>
<font color="#555555">025 </font> <font color="#00AA00">//List hinzufügen</font>
<font color="#555555">026 </font> List list = <font color="#0000AA">new</font> List();
<font color="#555555">027 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < 20; ++i) {
<font color="#555555">028 </font> list.add(<font color="#0000FF">"This is item "</font> + i);
<font color="#555555">029 </font> }
<font color="#555555">030 </font> gbc = makegbc(0, 0, 1, 3);
<font color="#555555">031 </font> gbc.weightx = 100;
<font color="#555555">032 </font> gbc.weighty = 100;
<font color="#555555">033 </font> gbc.fill = GridBagConstraints.BOTH;
<font color="#555555">034 </font> gbl.setConstraints(list, gbc);
<font color="#555555">035 </font> add(list);
<font color="#555555">036 </font> <font color="#00AA00">//Zwei Labels und zwei Textfelder</font>
<font color="#555555">037 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < 2; ++i) {
<font color="#555555">038 </font> <font color="#00AA00">//Label</font>
<font color="#555555">039 </font> gbc = makegbc(1, i, 1, 1);
<font color="#555555">040 </font> gbc.fill = GridBagConstraints.NONE;
<font color="#555555">041 </font> Label label = <font color="#0000AA">new</font> Label(<font color="#0000FF">"Label "</font> + (i + 1));
<font color="#555555">042 </font> gbl.setConstraints(label, gbc);
<font color="#555555">043 </font> add(label);
<font color="#555555">044 </font> <font color="#00AA00">//Textfeld</font>
<font color="#555555">045 </font> gbc = makegbc(2, i, 1, 1);
<font color="#555555">046 </font> gbc.weightx = 100;
<font color="#555555">047 </font> gbc.fill = GridBagConstraints.HORIZONTAL;
<font color="#555555">048 </font> TextField field = <font color="#0000AA">new</font> TextField(<font color="#0000FF">"TextField "</font> + (i +1));
<font color="#555555">049 </font> gbl.setConstraints(field, gbc);
<font color="#555555">050 </font> add(field);
<font color="#555555">051 </font> }
<font color="#555555">052 </font> <font color="#00AA00">//Ende-Button</font>
<font color="#555555">053 </font> Button button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"Ende"</font>);
<font color="#555555">054 </font> gbc = makegbc(2, 2, 0, 0);
<font color="#555555">055 </font> gbc.fill = GridBagConstraints.NONE;
<font color="#555555">056 </font> gbc.anchor = GridBagConstraints.SOUTHEAST;
<font color="#555555">057 </font> gbl.setConstraints(button, gbc);
<font color="#555555">058 </font> add(button);
<font color="#555555">059 </font> <font color="#00AA00">//Dialogelemente layouten</font>
<font color="#555555">060 </font> pack();
<font color="#555555">061 </font> }
<font color="#555555">062 </font>
<font color="#555555">063 </font> <font color="#0000AA">private</font> GridBagConstraints makegbc(
<font color="#555555">064 </font> <font color="#006699">int</font> x, <font color="#006699">int</font> y, <font color="#006699">int</font> width, <font color="#006699">int</font> height)
<font color="#555555">065 </font> {
<font color="#555555">066 </font> GridBagConstraints gbc = <font color="#0000AA">new</font> GridBagConstraints();
<font color="#555555">067 </font> gbc.gridx = x;
<font color="#555555">068 </font> gbc.gridy = y;
<font color="#555555">069 </font> gbc.gridwidth = width;
<font color="#555555">070 </font> gbc.gridheight = height;
<font color="#555555">071 </font> gbc.insets = <font color="#0000AA">new</font> Insets(1, 1, 1, 1);
<font color="#555555">072 </font> <font color="#0000AA">return</font> gbc;
<font color="#555555">073 </font> }
<font color="#555555">074 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3106.java"><font color="#000055" size=-1>Listing3106.java</font></a></td>
</tr>
</table>
<i>
Listing 31.6: Beispiel für GridBagLayout</i></p>
<p>
Das Programm besitzt eine kleine Hilfsmethode <font color="#000077"><tt>makegbc</tt></font>,
mit der auf einfache Weise ein neues <a href="index_g.html#ixb101617"><font color=#000080><tt>GridBagConstraints</tt></font></a>-Objekt
für einen vorgegebenen Bereich von Zellen erzeugt werden kann.
Die übrigen Membervariablen werden im Konstruktor der Fensterklasse
zugewiesen. <a href="k100203.html#gridbaglayout3">Abbildung 31.10</a>
zeigt das Fenster nach dem Skalieren in x- und y-Richtung.
<p>
<a name="gridbaglayout3"></a>
<img src="images/GridBagLayoutBsp2.gif">
<p>
<p><i>
Abbildung 31.10: Das GridBagLayout-Beispiel nach dem Skalieren</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Seit dem JDK 1.2 gibt es in der Klasse <a href="index_g.html#ixb101617"><font color=#000080><tt>GridBagConstraints</tt></font></a>
neben dem parameterlosen Konstruktor einen weiteren, mit dem alle
Membervariablen direkt beim Konstruktoraufruf gesetzt werden können.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id031002005"></a>
<h3>31.2.5 NULL-Layout </h3>
<p>
Ein <a name="ixa102145"><i>Null-Layout</i></a> wird erzeugt, indem
die Methode <a href="index_s.html#ixb101597"><font color=#000080><tt>setLayout</tt></font></a>
mit dem Argument <a href="index_n.html#ixb100235"><font color=#000080><tt>null</tt></font></a>
aufgerufen wird. In diesem Fall verwendet das Fenster keinen Layoutmanager,
sondern überläßt die Positionierung der Komponenten
der Anwendung. Für jedes einzufügende Dialogelement sind
dann drei Schritte auszuführen:
<ul>
<li>Das Dialogelement wird erzeugt.
<li>Seine Größe und Position werden festgelegt.
<li>Das Dialogelement wird an das Fenster übergeben.
</ul>
<p>
Der erste und letzte Schritt unterscheiden sich nicht von unseren
bisherigen Versuchen, bei denen wir vordefinierte Layoutmanager verwendet
haben. Im zweiten Schritt ist allerdings etwas Handarbeit notwendig.
Größe und Position des Dialogelements können mit den
Methoden <font color="#000077"><tt>move</tt></font> und <a name="ixa102146"><a href="index_s.html#ixb101160"><font color=#000080><tt>setSize</tt></font></a></a>
oder - noch einfacher - mit <a name="ixa102147"><a href="index_s.html#ixb101356"><font color=#000080><tt>setBounds</tt></font></a></a>
festgelegt 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 setBounds(
int x, int y, int width, int height
)
</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>
Der Punkt (<font color="#000077"><tt>x</tt></font>,<font color="#000077"><tt>y</tt></font>)
bestimmt die neue Anfangsposition des Dialogelements und (<font color="#000077"><tt>width</tt></font>,
<font color="#000077"><tt>height</tt></font>) seine Größe.
<p>
Das folgende Listing demonstriert diese Vorgehensweise am Beispiel
einer treppenartigen Anordnung von fünf Buttons:
<a name="listingid031007"></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">/* Listing3107.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> Listing3107
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3107 wnd = <font color="#0000AA">new</font> Listing3107();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3107()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Dialogelemente ohne Layoutmanager"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> setSize(300,250);
<font color="#555555">021 </font> setLayout(<font color="#006699">null</font>);
<font color="#555555">022 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < 5; ++i) {
<font color="#555555">023 </font> Button button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"Button"</font>+(i+1));
<font color="#555555">024 </font> button.setBounds(10+i*35,40+i*35,100,30);
<font color="#555555">025 </font> add(button);
<font color="#555555">026 </font> }
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3107.java"><font color="#000055" size=-1>Listing3107.java</font></a></td>
</tr>
</table>
<i>
Listing 31.7: Anordnen von Dialogelementen ohne Layoutmanager</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid031011"></a>
<img src="images/NullLayout.gif">
<p>
<p><i>
Abbildung 31.11: Verwendung des Null-Layouts</i></p>
<!-- Section -->
<a name="sectlevel3id031002006"></a>
<h3>31.2.6 Schachteln von Layoutmanagern <a name="ixa102148"></a>
</h3>
<p>
Eines der Schlüsselkonzepte zur Realisierung komplexer, portabler
Dialoge ist die Fähigkeit, Layoutmanager schachteln zu können.
Dazu wird an der Stelle, die ein Sublayout erhalten soll, einfach
ein Objekt der Klasse <a name="ixa102149"><a href="index_p.html#ixb101354"><font color=#000080><tt>Panel</tt></font></a></a>
eingefügt, das einen eigenen Layoutmanager erhält. Dieses
<a href="index_p.html#ixb101354"><font color=#000080><tt>Panel</tt></font></a>
kann mit Dialogelementen bestückt werden, die entsprechend dem
zugeordneten Unterlayout formatiert werden.
<p>
Wenn wir an die in <a href="k100174.html#hierarchiefensterklassen">Abbildung 27.1</a>
vorgestellte Klassenhierarchie innerhalb des AWT denken, werden wir
uns daran erinnern, dass ein <a href="index_p.html#ixb101354"><font color=#000080><tt>Panel</tt></font></a>
die einfachste konkrete Containerklasse ist. Sie erbt alle Eigenschaften
von <a href="index_c.html#ixb101353"><font color=#000080><tt>Container</tt></font></a>
und bietet damit die Möglichkeit, Komponenten aufzunehmen und
mit Hilfe eines Layoutmanagers auf dem Bildschirm anzuordnen.
<p>
Das folgende Beispiel zeigt einen Dialog, der ein <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
der Größe 1*2 Elemente verwendet. Innerhalb des ersten
Elements wird ein <a href="index_p.html#ixb101354"><font color=#000080><tt>Panel</tt></font></a>
mit einem <a href="index_g.html#ixb101599"><font color=#000080><tt>GridLayout</tt></font></a>
der Größe 3*1 Elemente verwendet, innerhalb des zweiten
Elements ein <a href="index_b.html#ixb101600"><font color=#000080><tt>BorderLayout</tt></font></a>:
<a name="listingid031008"></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">/* Listing3108.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> Listing3108
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3108 wnd = <font color="#0000AA">new</font> Listing3108();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3108()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Geschachtelte Layoutmanager"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> <font color="#00AA00">//Panel 1</font>
<font color="#555555">021 </font> Panel panel1 = <font color="#0000AA">new</font> Panel();
<font color="#555555">022 </font> panel1.setLayout(<font color="#0000AA">new</font> GridLayout(3,1));
<font color="#555555">023 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button1"</font>));
<font color="#555555">024 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button2"</font>));
<font color="#555555">025 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button3"</font>));
<font color="#555555">026 </font> <font color="#00AA00">//Panel 2</font>
<font color="#555555">027 </font> Panel panel2 = <font color="#0000AA">new</font> Panel();
<font color="#555555">028 </font> panel2.setLayout(<font color="#0000AA">new</font> BorderLayout());
<font color="#555555">029 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button4"</font>), BorderLayout.NORTH);
<font color="#555555">030 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button5"</font>), BorderLayout.SOUTH);
<font color="#555555">031 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button6"</font>), BorderLayout.WEST);
<font color="#555555">032 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button7"</font>), BorderLayout.EAST);
<font color="#555555">033 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button8"</font>), BorderLayout.CENTER);
<font color="#555555">034 </font> <font color="#00AA00">//Hauptfenster</font>
<font color="#555555">035 </font> setLayout(<font color="#0000AA">new</font> GridLayout(1,2));
<font color="#555555">036 </font> add(panel1);
<font color="#555555">037 </font> add(panel2);
<font color="#555555">038 </font> pack();
<font color="#555555">039 </font> }
<font color="#555555">040 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3108.java"><font color="#000055" size=-1>Listing3108.java</font></a></td>
</tr>
</table>
<i>
Listing 31.8: Schachteln von Layoutmanagern</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid031012"></a>
<img src="images/SchachtelLayout1.gif">
<p>
<p><i>
Abbildung 31.12: Verwendung eines geschachtelten Layouts</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Geschachtelte Layouts können auch dann sinnvoll verwendet werden,
wenn innerhalb der Bestandteile eines <font color="#000077"><tt>BorderLayouts</tt></font>
<i>mehrere</i> Komponenten untergebracht werden sollen oder wenn die
Komponenten eines <i>skalierenden</i> Layoutmanagers ihre ursprüngliche
Größe beibehalten sollen.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#0099CC">
<tr>
<td><font color="#FFFFFF"> Tipp </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Das folgende Beispiel demonstriert beide Anwendungen anhand eines
<font color="#000077"><tt>BorderLayouts</tt></font>, das im South-Element
zwei rechtsbündig angeordnete Buttons in Originalgröße
und im Center-Element sechs innerhalb eines <font color="#000077"><tt>GridLayouts</tt></font>
der Größe 3*2 Elemente angeordnete, skalierte Buttons enthält:
<a name="listingid031009"></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">/* Listing3109.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> Listing3109
<font color="#555555">007 </font><font color="#0000AA">extends</font> Frame
<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> Listing3109 wnd = <font color="#0000AA">new</font> Listing3109();
<font color="#555555">012 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> Listing3109()
<font color="#555555">016 </font> {
<font color="#555555">017 </font> <font color="#006699">super</font>(<font color="#0000FF">"Geschachtelte Layoutmanager, Teil II"</font>);
<font color="#555555">018 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">019 </font> <font color="#00AA00">//Layout setzen und Komponenten hinzufügen</font>
<font color="#555555">020 </font> setSize(300,200);
<font color="#555555">021 </font> <font color="#00AA00">//Panel 1</font>
<font color="#555555">022 </font> Panel panel1 = <font color="#0000AA">new</font> Panel();
<font color="#555555">023 </font> panel1.setLayout(<font color="#0000AA">new</font> GridLayout(3,2));
<font color="#555555">024 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button1"</font>));
<font color="#555555">025 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button2"</font>));
<font color="#555555">026 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button3"</font>));
<font color="#555555">027 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button4"</font>));
<font color="#555555">028 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button5"</font>));
<font color="#555555">029 </font> panel1.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Button6"</font>));
<font color="#555555">030 </font> <font color="#00AA00">//Panel 2</font>
<font color="#555555">031 </font> Panel panel2 = <font color="#0000AA">new</font> Panel();
<font color="#555555">032 </font> panel2.setLayout(<font color="#0000AA">new</font> FlowLayout(FlowLayout.RIGHT));
<font color="#555555">033 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"Abbruch"</font>));
<font color="#555555">034 </font> panel2.add(<font color="#0000AA">new</font> Button(<font color="#0000FF">"OK"</font>));
<font color="#555555">035 </font> <font color="#00AA00">//Hauptfenster</font>
<font color="#555555">036 </font> setLayout(<font color="#0000AA">new</font> BorderLayout());
<font color="#555555">037 </font> add(panel1, BorderLayout.CENTER);
<font color="#555555">038 </font> add(panel2, BorderLayout.SOUTH);
<font color="#555555">039 </font> }
<font color="#555555">040 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3109.java"><font color="#000055" size=-1>Listing3109.java</font></a></td>
</tr>
</table>
<i>
Listing 31.9: Eine weitere Anwendung für geschachtelte Layoutmanager</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid031013"></a>
<img src="images/SchachtelLayout2.gif">
<p>
<p><i>
Abbildung 31.13: Ein weiteres Beispiel für geschachtelte Layouts</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"> 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="k100201.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100202.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100204.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100206.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>
|