blob: 76f17f410c5f289a07d91307668ec300a6209e1b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/* Listing3709.java */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Listing3709
extends JFrame
implements ActionListener
{
private ButtonGroup group = new ButtonGroup();
public Listing3709()
{
super("JRadioButton");
addWindowListener(new WindowClosingAdapter(true));
//RadioButton-Panel
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 1));
for (int i = 1; i <= 3; ++i) {
JRadioButton rb = new JRadioButton("RadioButton" + i, i == 2);
rb.setActionCommand(rb.getText());
panel.add(rb);
group.add(rb);
}
getContentPane().add(panel, BorderLayout.CENTER);
//Selektion-Button
JButton button = new JButton("Selektion");
button.addActionListener(this);
getContentPane().add(button, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if (cmd.equals("Selektion")) {
ButtonModel selected = group.getSelection();
System.out.print("Selektiert: ");
if (selected != null) {
System.out.println(selected.getActionCommand());
}
}
}
public static void main(String[] args)
{
Listing3709 frame = new Listing3709();
frame.setLocation(100, 100);
frame.setSize(300, 120);
frame.setVisible(true);
}
}
|