import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JToggleButton; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; public class ButtonGroupDemo { public ButtonGroupDemo() { initGUI(); } private void initGUI() { JPanel rbGroupPanel = new JPanel(new FlowLayout()); ButtonGroup bg = new ButtonGroup(); JRadioButton rbg1 = new JRadioButton(); bg.add(rbg1); rbGroupPanel.add(rbg1); JRadioButton rbg2 = new JRadioButton(); bg.add(rbg2); rbGroupPanel.add(rbg2); JRadioButton rbg3 = new JRadioButton(); bg.add(rbg3); rbGroupPanel.add(rbg3); JPanel cbGroupPanel = new JPanel(new FlowLayout()); ButtonGroup bg1 = new ButtonGroup(); JCheckBox cbg1 = new JCheckBox(); bg1.add(cbg1); cbGroupPanel.add(cbg1); JCheckBox cbg2 = new JCheckBox(); bg1.add(cbg2); cbGroupPanel.add(cbg2); JCheckBox cbg3 = new JCheckBox(); bg1.add(cbg3); cbGroupPanel.add(cbg3); JPanel tbGroupPanel = new JPanel(new FlowLayout()); ButtonGroup bg2 = new ButtonGroup(); JToggleButton tbg1 = new JToggleButton("1"); bg2.add(tbg1); tbGroupPanel.add(tbg1); JToggleButton tbg2 = new JToggleButton("2"); bg2.add(tbg2); tbGroupPanel.add(tbg2); JToggleButton tbg3 = new JToggleButton("3"); bg2.add(tbg3); tbGroupPanel.add(tbg3); JPanel rbNoGroupPanel = new JPanel(new FlowLayout()); JRadioButton rbn1 = new JRadioButton(); rbNoGroupPanel.add(rbn1); JRadioButton rbn2 = new JRadioButton(); rbNoGroupPanel.add(rbn2); JRadioButton rbn3 = new JRadioButton(); rbNoGroupPanel.add(rbn3); JPanel cbNoGroupPanel = new JPanel(new FlowLayout()); JCheckBox cb1 = new JCheckBox(); cbNoGroupPanel.add(cb1); JCheckBox cb2 = new JCheckBox(); cbNoGroupPanel.add(cb2); JCheckBox cb3 = new JCheckBox(); cbNoGroupPanel.add(cb3); JPanel tbNoGroupPanel = new JPanel(new FlowLayout()); JToggleButton tb1 = new JToggleButton("1"); tbNoGroupPanel.add(tb1); JToggleButton tb2 = new JToggleButton("2"); tbNoGroupPanel.add(tb2); JToggleButton tb3 = new JToggleButton("3"); tbNoGroupPanel.add(tb3); JFrame frame = new JFrame("ButtonGroupDemo"); frame.setLayout(new GridLayout(8, 1)); JLabel gLabel = new JLabel("Gruppiert:"); gLabel.setBorder(new EmptyBorder(0, 5, 0, 5)); frame.add(gLabel); frame.add(rbGroupPanel); frame.add(cbGroupPanel); frame.add(tbGroupPanel); JLabel nLabel = new JLabel("Nicht gruppiert:"); nLabel.setBorder(new EmptyBorder(0, 5, 0, 5)); frame.add(nLabel); frame.add(rbNoGroupPanel); frame.add(cbNoGroupPanel); frame.add(tbNoGroupPanel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new ButtonGroupDemo()); } }