Radio1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
JRadioButton demo.
*/
public class Radio1 extends JFrame
{
    public static void main(String[] argv)
    {
        Radio1 t = new Radio1();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.show();
    }

    public Radio1()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension ss = kit.getScreenSize();
        setSize(ss.width/2,ss.height/6);
        setLocation(ss.width/4,ss.height/4);
        setTitle("CISC370-011 -- JRadioButton demo");

        Container cp = getContentPane();
        JPanel p = new JPanel();
        cp.add(p);

        JLabel label = new JLabel("Pick your favorite stooge:");
        cp.add(label,BorderLayout.NORTH);

        String[] labels = {
            "Moe",
            "Larry",
            "Curly",
            "Joe",
            };
        ButtonGroup group = new ButtonGroup();
        for (int i=0; i<labels.length; i++) {
            JRadioButton button = new JRadioButton(labels[i]);
            p.add(button);
            group.add(button);
            button.addActionListener(new Checker(labels[i]));
            if (i == 0)
                button.setSelected(true);
            }
    }
}


/**
An ActionListener to watch for changes in a JRadioButton.
*/
class Checker implements ActionListener
{
    public Checker(String name)
        { this.name = name; }

    public void actionPerformed(ActionEvent e)
        { System.out.println("Selected " + name); }

    private String name;
}