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

    // Display two grouped JRadioButton components.
    // Respond to events on one of them.
    // The text of a status label is changed when the state of the
    // Yes button is changed.
class SwingRadioButtonFrame extends JFrame {
    public SwingRadioButtonFrame() {
        final int width = 300, height = 150;
        setSize(width,height);
        Container contents = getContentPane();

        // The status label and strings for it.
        final String willHear = "You will be hearing from us.",
                     wontHear = "You won't be hearing from us.";
        final JLabel status = new JLabel(wontHear,JLabel.CENTER);

        // A question to prompt a Yes/No answer.
        final JLabel question =
                        new JLabel("Would you like to receive email from us?",
                                   JLabel.CENTER);
        // Create the radio buttons. One listens for
        // changes and modifies the status label's string.
        // Initially the no button is selected.
        final JRadioButton yesButton = new JRadioButton("Yes",false),
                           noButton  = new JRadioButton("No",true);

        // Make the text in the status label respond to changes of state.
        yesButton.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){
                int change = e.getStateChange();
                if(change == ItemEvent.SELECTED){
                    status.setText(willHear);
                }
                else{
                    status.setText(wontHear);
                }
            }
        });

        // Group the buttons to achieve mutual exclusivity.
        ButtonGroup answerGroup = new ButtonGroup();
        answerGroup.add(yesButton);
        answerGroup.add(noButton);

        // Group the buttons in their own panel.
        JPanel answerPanel = new JPanel();
        answerPanel.add(yesButton);
        answerPanel.add(noButton);

        // Add the components to a central panel in a single column.
        final JPanel radioPanel = new JPanel();
        final int rows = 3, cols = 1;
        radioPanel.setLayout(new GridLayout(rows,cols));
        radioPanel.add(question);
        radioPanel.add(answerPanel);
        radioPanel.add(status);

        contents.add(radioPanel,"Center");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new QuitJButton());
        contents.add(buttonPanel,"South");
    }
}
