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
|
<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,k100237.html;106,k100238.html;107,k100240.html;108,k100243.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="k100237.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100238.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100240.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100243.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 37 - Swing: Komponenten I
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id037002"></a>
<h2>37.2 Buttons </h2>
<hr>
<ul>
<li><a href="k100239.html#sectlevel2id037002">37.2 Buttons</a>
<ul>
<li><a href="k100239.html#sectlevel3id037002001">37.2.1 JButton</a>
<li><a href="k100239.html#sectlevel3id037002002">37.2.2 JCheckBox</a>
<li><a href="k100239.html#sectlevel3id037002003">37.2.3 JRadioButton</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id037002001"></a>
<h3>37.2.1 JButton </h3>
<p>
Swing-Buttons sind uns in den vorangegangenen Abschnitten schon mehrfach
begegnet. Sie sind Instanzen der Klasse <a name="ixa102608"><a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a></a>
und dienen dazu, Schaltflächen zu erzeugen. Die wichtigsten Konstruktoren
von <a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JButton(String text)
public JButton(Icon icon)
public JButton(String text, Icon icon)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JButton.html" onClick="this.href=getApiDoc('javax.swing.JButton')"><font color="#660066" size=-1>javax.swing.JButton</font></a></td>
</tr>
</table>
<p>
Ein Button kann also wahlweise mit Beschriftung, mit Icon oder mit
beidem instanziert werden. Auf die Anordnung eines Buttons und seiner
Bestandteile kann mit Hilfe der schon aus <a href="k100238.html#jlabel">Abschnitt 37.1.1</a>
bekannten Methoden zugegriffen werden: <a name="ixa102609"></a> <a name="ixa102610"></a>
<a name="ixa102611"></a> <a name="ixa102612"></a> <a name="ixa102613"></a>
<a name="ixa102614"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setHorizontalTextPosition(int textPosition)
public int getHorizontalTextPosition()
public void setHorizontalAlignment(int alignment)
public int getHorizontalAlignment()
public void setVerticalAlignment(int alignment)
public int getVerticalAlignment()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JButton.html" onClick="this.href=getApiDoc('javax.swing.JButton')"><font color="#660066" size=-1>javax.swing.JButton</font></a></td>
</tr>
</table>
<p>
<a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
ist wie <a href="index_j.html#ixb101831"><font color=#000080><tt>JMenuItem</tt></font></a>
(siehe <a href="k100234.html#einfachemenues">Abschnitt 36.2.1</a>)
aus <a name="ixa102615"><a href="index_a.html#ixb101781"><font color=#000080><tt>AbstractButton</tt></font></a></a>
abgeleitet und stellt ebenfalls die Methoden <a name="ixa102616"><a href="index_s.html#ixb101832"><font color=#000080><tt>setMnemonic</tt></font></a></a>,
<a name="ixa102617"><a href="index_g.html#ixb101833"><font color=#000080><tt>getMnemonic</tt></font></a></a>,
<a name="ixa102618"><a href="index_s.html#ixb101359"><font color=#000080><tt>setEnabled</tt></font></a></a>
und <a name="ixa102619"><a href="index_i.html#ixb101360"><font color=#000080><tt>isEnabled</tt></font></a></a>
zur Verfügung. Wird ein <a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
per Mausklick betätigt, sendet er ein <a href="index_a.html#ixb101404"><font color=#000080><tt>ActionEvent</tt></font></a>
an alle registrierten Listener. Durch Aufruf von <a name="ixa102620"><a href="index_d.html#ixb101936"><font color=#000080><tt>doClick</tt></font></a></a>
kann ein Buttonklick auch programmgesteuert ausgelöst werden.
<p>
Das Beispielprogramm in <a href="k100239.html#jbuttonbeispiel">Listing 37.7</a>
erzeugt drei Buttons und ordnet sie nebeneinander in einem <a href="index_j.html#ixb100511"><font color=#000080><tt>JFrame</tt></font></a>
an:
<ul>
<li>Der »OK«-Button ist eine Instanz unserer eigenen (aus
<a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
abgeleiteten) Klasse <a name="ixa102621"><a href="index_d.html#ixb101937"><font color=#000080><tt>DefaultButton</tt></font></a></a>.
Seine Besonderheit besteht darin, dass er sich durch Aufruf von <a name="ixa102622"><a href="index_s.html#ixb101938"><font color=#000080><tt>setDefaultButton</tt></font></a></a>
bei der an den Konstruktor übergebenen RootPane registriert.
Dadurch kann er - unabhängig davon, welches Dialogelement gerade
den Fokus hat - auch durch Drücken von <font color="#007700"><tt>[ENTER]</tt></font>
betätigt werden.
<li>Der »Abbrechen«-Button ist eine Instanz der ebenfalls
selbstdefinierten und aus <a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
abgeleiteten Klasse <a name="ixa102623"><a href="index_c.html#ixb101939"><font color=#000080><tt>CancelButton</tt></font></a></a>.
Seine Besonderheit besteht darin, dass er beim Drücken von <font color="#007700"><tt>[ESCAPE]</tt></font>
die Methode <a href="index_d.html#ixb101936"><font color=#000080><tt>doClick</tt></font></a>
aufruft und dadurch einen Buttonklick simuliert.
<li>Der »Hilfe«-Button ist eine direkte Instanzierung von
<a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>.
Er definiert ein Mnemonik für die Tastenkombination <font color="#007700"><tt>[ALT]</tt></font>+<font color="#007700"><tt>[H]</tt></font>,
registriert den <a href="index_a.html#ixb101469"><font color=#000080><tt>ActionListener</tt></font></a>
und wird dann der ContentPane hinzugefügt.
</ul>
<p>
Wir wollen uns zunächst den Code für die Klasse <a href="index_d.html#ixb101937"><font color=#000080><tt>DefaultButton</tt></font></a>
ansehen:
<a name="listingid037005"></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">/* DefaultButton.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> javax.swing.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> DefaultButton
<font color="#555555">006 </font><font color="#0000AA">extends</font> JButton
<font color="#555555">007 </font>{
<font color="#555555">008 </font> <font color="#0000AA">public</font> DefaultButton(String title, JRootPane rootpane)
<font color="#555555">009 </font> {
<font color="#555555">010 </font> <font color="#006699">super</font>(title);
<font color="#555555">011 </font> rootpane.setDefaultButton(<font color="#006699">this</font>);
<font color="#555555">012 </font> }
<font color="#555555">013 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/DefaultButton.java"><font color="#000055" size=-1>DefaultButton.java</font></a></td>
</tr>
</table>
<i>
Listing 37.5: Die Klasse DefaultButton</i></p>
<p>
Auch die Klasse <a href="index_c.html#ixb101939"><font color=#000080><tt>CancelButton</tt></font></a>
ist ähnlich einfach aufgebaut:
<a name="listingid037006"></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">/* CancelButton.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> javax.swing.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> CancelButton
<font color="#555555">007 </font><font color="#0000AA">extends</font> JButton
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#0000AA">public</font> CancelButton(String title)
<font color="#555555">010 </font> {
<font color="#555555">011 </font> <font color="#006699">super</font>(title);
<font color="#555555">012 </font> ActionListener al = <font color="#0000AA">new</font> ActionListener()
<font color="#555555">013 </font> {
<font color="#555555">014 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">015 </font> {
<font color="#555555">016 </font> String cmd = event.getActionCommand();
<font color="#555555">017 </font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"PressedESCAPE"</font>)) {
<font color="#555555">018 </font> doClick();
<font color="#555555">019 </font> }
<font color="#555555">020 </font> }
<font color="#555555">021 </font> };
<font color="#555555">022 </font> registerKeyboardAction(
<font color="#555555">023 </font> al,
<font color="#555555">024 </font> <font color="#0000FF">"PressedESCAPE"</font>,
<font color="#555555">025 </font> KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
<font color="#555555">026 </font> JButton.WHEN_IN_FOCUSED_WINDOW
<font color="#555555">027 </font> );
<font color="#555555">028 </font> }
<font color="#555555">029 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/CancelButton.java"><font color="#000055" size=-1>CancelButton.java</font></a></td>
</tr>
</table>
<i>
Listing 37.6: Die Klasse CancelButton</i></p>
<p>
Nun können wir uns den Programmcode unseres Beispielprogramms
ansehen:
<a name="jbuttonbeispiel"></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">/* Listing3707.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> javax.swing.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3707
<font color="#555555">008 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">009 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">010 </font>{
<font color="#555555">011 </font> <font color="#0000AA">public</font> Listing3707()
<font color="#555555">012 </font> {
<font color="#555555">013 </font> <font color="#006699">super</font>(<font color="#0000FF">"JButton"</font>);
<font color="#555555">014 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">015 </font> Container cp = getContentPane();
<font color="#555555">016 </font> cp.setLayout(<font color="#0000AA">new</font> FlowLayout());
<font color="#555555">017 </font> JPanel panel = <font color="#0000AA">new</font> JPanel();
<font color="#555555">018 </font> <font color="#00AA00">//OK-Button</font>
<font color="#555555">019 </font> JButton okButton = <font color="#0000AA">new</font> DefaultButton(<font color="#0000FF">"OK"</font>, getRootPane());
<font color="#555555">020 </font> okButton.addActionListener(<font color="#006699">this</font>);
<font color="#555555">021 </font> panel.add(okButton);
<font color="#555555">022 </font> <font color="#00AA00">//Abbrechen-Button</font>
<font color="#555555">023 </font> JButton cancelButton = <font color="#0000AA">new</font> CancelButton(<font color="#0000FF">"Abbrechen"</font>);
<font color="#555555">024 </font> cancelButton.addActionListener(<font color="#006699">this</font>);
<font color="#555555">025 </font> panel.add(cancelButton);
<font color="#555555">026 </font> <font color="#00AA00">//Hilfe-Button</font>
<font color="#555555">027 </font> JButton helpButton = <font color="#0000AA">new</font> JButton(<font color="#0000FF">"Hilfe"</font>);
<font color="#555555">028 </font> helpButton.setMnemonic(<font color="#0000FF">'H'</font>);
<font color="#555555">029 </font> helpButton.addActionListener(<font color="#006699">this</font>);
<font color="#555555">030 </font> panel.add(helpButton);
<font color="#555555">031 </font> <font color="#00AA00">//Panel hinzufügen</font>
<font color="#555555">032 </font> cp.add(panel);
<font color="#555555">033 </font> }
<font color="#555555">034 </font>
<font color="#555555">035 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">036 </font> {
<font color="#555555">037 </font> System.out.println(event.getActionCommand());
<font color="#555555">038 </font> }
<font color="#555555">039 </font>
<font color="#555555">040 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">041 </font> {
<font color="#555555">042 </font> Listing3707 frame = <font color="#0000AA">new</font> Listing3707();
<font color="#555555">043 </font> frame.setLocation(100, 100);
<font color="#555555">044 </font> frame.setSize(300, 100);
<font color="#555555">045 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">046 </font> }
<font color="#555555">047 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3707.java"><font color="#000055" size=-1>Listing3707.java</font></a></td>
</tr>
</table>
<i>
Listing 37.7: Die Klasse JButton</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid037005"></a>
<img src="images/JButton1.gif">
<p>
<p><i>
Abbildung 37.5: Die Klasse JButton</i></p>
<!-- Section -->
<a name="sectlevel3id037002002"></a>
<h3>37.2.2 JCheckBox </h3>
<p>
Die Klasse <a name="ixa102624"><a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a></a>
stellt einen Button dar, der vom Anwender wahlweise an- oder ausgeschaltet
werden kann. Er wird meist verwendet, um boolesche Werte auf einer
GUI-Oberfläche darzustellen. <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
ist von der Klasse <a name="ixa102625"><a href="index_j.html#ixb101941"><font color=#000080><tt>JToggleButton</tt></font></a></a>
abgeleitet, die als Abstraktion von Buttons, die ihren Zustand ändern
können, auch Basisklasse von <a href="index_j.html#ixb101942"><font color=#000080><tt>JRadioButton</tt></font></a>
ist.
<p>
<a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
kann eine textuelle Beschriftung oder ein Icon oder beides enthalten.
Die wichtigsten Konstruktoren sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JCheckBox(String text)
public JCheckBox(String text, boolean selected)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JCheckBox.html" onClick="this.href=getApiDoc('javax.swing.JCheckBox')"><font color="#660066" size=-1>javax.swing.JCheckBox</font></a></td>
</tr>
</table>
<p>
Mit dem Parameter <font color="#000077"><tt>selected</tt></font> kann
bereits bei der Instanzierung angegeben werden, ob die Checkbox aktiviert
oder deaktiviert sein soll. Später kann der Aktivierungszustand
mit <a name="ixa102626"><a href="index_s.html#ixb101858"><font color=#000080><tt>setSelected</tt></font></a></a>
gesetzt und mit <a name="ixa102627"><a href="index_i.html#ixb101857"><font color=#000080><tt>isSelected</tt></font></a></a>
abgefragt 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 setSelected(boolean b)
public boolean isSelected()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JCheckBox.html" onClick="this.href=getApiDoc('javax.swing.JCheckBox')"><font color="#660066" size=-1>javax.swing.JCheckBox</font></a></td>
</tr>
</table>
<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>
Bei der Instanzierung einer <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
kann auch ein Icon übergeben werden. Das ist allerdings nur in
Ausnahmefällen sinnvoll, denn es wird nicht zusätzlich zum
Ankreuzfeld angezeigt, sondern statt dessen. Das Ankreuzfeld einer
<a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
ist also gerade ihr (automatisch angezeigtes) Icon. Durch Aufruf von
<a href="index_s.html#ixb101844"><font color=#000080><tt>setHorizontalTextPosition</tt></font></a>
und Übergabe der Konstante <a href="index_l.html#ixb101614"><font color=#000080><tt>LEFT</tt></font></a>
können Ankreuzfeld und Text vertauscht 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>
<p>
Wie ein <a href="index_j.html#ixb101780"><font color=#000080><tt>JButton</tt></font></a>
sendet eine <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
bei jeder Betätigung ein <a href="index_a.html#ixb101404"><font color=#000080><tt>ActionEvent</tt></font></a>
an registrierte Listener. Zudem wird bei Zustandsänderungen ein
<a href="index_i.html#ixb101406"><font color=#000080><tt>ItemEvent</tt></font></a>
versendet, auf das ein <a href="index_i.html#ixb101481"><font color=#000080><tt>ItemListener</tt></font></a>
reagieren kann.
<p>
Das folgende Programm erzeugt drei Checkboxen in einem Panel und gibt
alle Zustandsänderungen auf der Konsole aus:
<a name="listingid037008"></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">/* Listing3708.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> javax.swing.*;
<font color="#555555">006 </font><font color="#0000AA">import</font> javax.swing.event.*;
<font color="#555555">007 </font>
<font color="#555555">008 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3708
<font color="#555555">009 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">010 </font><font color="#0000AA">implements</font> ItemListener
<font color="#555555">011 </font>{
<font color="#555555">012 </font> <font color="#0000AA">public</font> Listing3708()
<font color="#555555">013 </font> {
<font color="#555555">014 </font> <font color="#006699">super</font>(<font color="#0000FF">"JCheckBox"</font>);
<font color="#555555">015 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">016 </font> JPanel panel = <font color="#0000AA">new</font> JPanel();
<font color="#555555">017 </font> panel.setLayout(<font color="#0000AA">new</font> GridLayout(3, 1));
<font color="#555555">018 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i <= 3; ++i) {
<font color="#555555">019 </font> JCheckBox cb = <font color="#0000AA">new</font> JCheckBox(<font color="#0000FF">"Checkbox"</font> + i, i == 2);
<font color="#555555">020 </font> cb.addItemListener(<font color="#006699">this</font>);
<font color="#555555">021 </font> panel.add(cb);
<font color="#555555">022 </font> }
<font color="#555555">023 </font> getContentPane().add(panel, BorderLayout.CENTER);
<font color="#555555">024 </font> }
<font color="#555555">025 </font>
<font color="#555555">026 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> itemStateChanged(ItemEvent e)
<font color="#555555">027 </font> {
<font color="#555555">028 </font> JCheckBox cb = (JCheckBox)e.getSource();
<font color="#555555">029 </font> <font color="#006699">int</font> change = e.getStateChange();
<font color="#555555">030 </font> <font color="#0000AA">if</font> (change == ItemEvent.SELECTED) {
<font color="#555555">031 </font> System.out.println(cb.getText() + <font color="#0000FF">": SELECTED"</font>);
<font color="#555555">032 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (change == ItemEvent.DESELECTED) {
<font color="#555555">033 </font> System.out.println(cb.getText() + <font color="#0000FF">": DESELECTED"</font>);
<font color="#555555">034 </font> }
<font color="#555555">035 </font> }
<font color="#555555">036 </font>
<font color="#555555">037 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">038 </font> {
<font color="#555555">039 </font> Listing3708 frame = <font color="#0000AA">new</font> Listing3708();
<font color="#555555">040 </font> frame.setLocation(100, 100);
<font color="#555555">041 </font> frame.setSize(300, 100);
<font color="#555555">042 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">043 </font> }
<font color="#555555">044 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3708.java"><font color="#000055" size=-1>Listing3708.java</font></a></td>
</tr>
</table>
<i>
Listing 37.8: Die Klasse JCheckbox</i></p>
<p>
Die Programmausgabe ist:
<p>
<a name="imageid037006"></a>
<img src="images/JCheckBox1.gif">
<p>
<p><i>
Abbildung 37.6: Die Klasse JCheckBox</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>
Vorsicht, Schreibfehler! Anders als in der Checkbox-Klasse des AWT
(sie heißt <a href="index_c.html#ixb101483"><font color=#000080><tt>Checkbox</tt></font></a>),
wird das "Box" in <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
nicht klein-, sondern großgeschrieben.</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="sectlevel3id037002003"></a>
<h3>37.2.3 JRadioButton </h3>
<p>
Die Klasse <a name="ixa102628"><a href="index_j.html#ixb101942"><font color=#000080><tt>JRadioButton</tt></font></a></a>
ähnelt der Klasse <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>.
Auch sie stellt einen Button dar, der wahlweise an- oder ausgeschaltet
werden kann. Anders als bei <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
ist in einer Gruppe von Radiobuttons allerdings immer nur ein Button
zur Zeit aktiviert, alle anderen sind deaktiviert. Die wichtigsten
Konstruktoren von <a href="index_j.html#ixb101942"><font color=#000080><tt>JRadioButton</tt></font></a>
sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JRadioButton(String text)
public JRadioButton(String text, boolean selected)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JRadioButton.html" onClick="this.href=getApiDoc('javax.swing.JRadioButton')"><font color="#660066" size=-1>javax.swing.JRadioButton</font></a></td>
</tr>
</table>
<p>
Die übrigen Methoden können so verwendet werden, wie es
bei <a href="index_j.html#ixb101940"><font color=#000080><tt>JCheckBox</tt></font></a>
beschrieben wurde. Um die Buttons zu gruppieren, ist eine <a name="ixa102629"><a href="index_b.html#ixb101856"><font color=#000080><tt>ButtonGroup</tt></font></a></a>
zu instanzieren, und die Buttons sind durch Aufruf von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
hinzuzufügen. Mit <a name="ixa102630"><a href="index_g.html#ixb101943"><font color=#000080><tt>getSelection</tt></font></a></a>
kann auf das <a name="ixa102631"><a href="index_b.html#ixb101944"><font color=#000080><tt>ButtonModel</tt></font></a></a>
des selektierten Elements zugegriffen werden, <a name="ixa102632"><a href="index_g.html#ixb101945"><font color=#000080><tt>getElements</tt></font></a></a>
liefert alle Buttons der Gruppe:
<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(AbstractButton b)
public ButtonModel getSelection()
public Enumeration getElements()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/ButtonGroup.html" onClick="this.href=getApiDoc('javax.swing.ButtonGroup')"><font color="#660066" size=-1>javax.swing.ButtonGroup</font></a></td>
</tr>
</table>
<p>
Das folgende Programm enthält drei Radiobuttons, die über
eine <a href="index_b.html#ixb101856"><font color=#000080><tt>ButtonGroup</tt></font></a>
gruppiert werden. Der zusätzliche Button »Selektion«
ermittelt das selektierte Element und gibt dessen Kommandonamen auf
der Konsole aus:
<a name="listingid037009"></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">/* Listing3709.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> javax.swing.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3709
<font color="#555555">008 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">009 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">010 </font>{
<font color="#555555">011 </font> <font color="#0000AA">private</font> ButtonGroup group = <font color="#0000AA">new</font> ButtonGroup();
<font color="#555555">012 </font>
<font color="#555555">013 </font> <font color="#0000AA">public</font> Listing3709()
<font color="#555555">014 </font> {
<font color="#555555">015 </font> <font color="#006699">super</font>(<font color="#0000FF">"JRadioButton"</font>);
<font color="#555555">016 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">017 </font> <font color="#00AA00">//RadioButton-Panel</font>
<font color="#555555">018 </font> JPanel panel = <font color="#0000AA">new</font> JPanel();
<font color="#555555">019 </font> panel.setLayout(<font color="#0000AA">new</font> GridLayout(3, 1));
<font color="#555555">020 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i <= 3; ++i) {
<font color="#555555">021 </font> JRadioButton rb = <font color="#0000AA">new</font> JRadioButton(<font color="#0000FF">"RadioButton"</font> + i, i == 2);
<font color="#555555">022 </font> rb.setActionCommand(rb.getText());
<font color="#555555">023 </font> panel.add(rb);
<font color="#555555">024 </font> group.add(rb);
<font color="#555555">025 </font> }
<font color="#555555">026 </font> getContentPane().add(panel, BorderLayout.CENTER);
<font color="#555555">027 </font> <font color="#00AA00">//Selektion-Button</font>
<font color="#555555">028 </font> JButton button = <font color="#0000AA">new</font> JButton(<font color="#0000FF">"Selektion"</font>);
<font color="#555555">029 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">030 </font> getContentPane().add(button, BorderLayout.SOUTH);
<font color="#555555">031 </font> }
<font color="#555555">032 </font>
<font color="#555555">033 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">034 </font> {
<font color="#555555">035 </font> String cmd = event.getActionCommand();
<font color="#555555">036 </font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"Selektion"</font>)) {
<font color="#555555">037 </font> ButtonModel selected = group.getSelection();
<font color="#555555">038 </font> System.out.print(<font color="#0000FF">"Selektiert: "</font>);
<font color="#555555">039 </font> <font color="#0000AA">if</font> (selected != <font color="#006699">null</font>) {
<font color="#555555">040 </font> System.out.println(selected.getActionCommand());
<font color="#555555">041 </font> }
<font color="#555555">042 </font> }
<font color="#555555">043 </font> }
<font color="#555555">044 </font>
<font color="#555555">045 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">046 </font> {
<font color="#555555">047 </font> Listing3709 frame = <font color="#0000AA">new</font> Listing3709();
<font color="#555555">048 </font> frame.setLocation(100, 100);
<font color="#555555">049 </font> frame.setSize(300, 120);
<font color="#555555">050 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">051 </font> }
<font color="#555555">052 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3709.java"><font color="#000055" size=-1>Listing3709.java</font></a></td>
</tr>
</table>
<i>
Listing 37.9: Die Klasse JRadioButton</i></p>
<p>
Die Programmausgabe ist:
<p>
<a name="imageid037007"></a>
<img src="images/JRadioButton1.gif">
<p>
<p><i>
Abbildung 37.7: Die Klasse JRadioButton</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="k100237.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100238.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100240.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100243.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>
|