package FunGUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class RadioButton extends JFrame
{
// declare radio buttons.
private JRadioButton jrbRed, jrbGreen, jrbBlue, jrbWhite;
JTextArea txaList;
public static void main (String []args)
{
RadioButton gui = new RadioButton();
gui.setSize(500,250);
gui.setVisible(true);
gui.setTitle("Radio Button");
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
}
public RadioButton ()
{
txaList = new JTextArea(10,20);
// final JPanel pnlTshirt =
// new JPanel ( new BorderLayout(100, 200));
// add(pnlTshirt, BorderLayout.CENTER);
JPanel pnlRadioButtons = new JPanel ();
pnlRadioButtons.setLayout(new GridLayout (4, 1));
pnlRadioButtons.add(jrbRed = new JRadioButton ("Red"));
pnlRadioButtons.add(jrbGreen = new JRadioButton ("Green"));
pnlRadioButtons.add(jrbBlue = new JRadioButton ("Blue"));
pnlRadioButtons.add(jrbWhite= new JRadioButton ("White"));
// Create a radio button group to group 4 buttons.
ButtonGroup group = new ButtonGroup ();
group.add(jrbRed);
group.add(jrbGreen);
group.add(jrbBlue);
group.add(jrbWhite);
// JPanel pnlNorth = new JPanel (new FlowLayout());
// pnlNorth.add(group);
// Set keyboard mnemonics.
jrbRed.setMnemonic('R');
jrbGreen.setMnemonic('G');
jrbBlue.setMnemonic('B');
jrbWhite.setMnemonic('W');
//Register listeners for radio button.
jrbRed.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
txaList.setForeground(Color.red);
}
});
jrbGreen.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
txaList.setForeground(Color.green);
}
});
jrbBlue.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
txaList.setForeground(Color.blue);
}
});
jrbWhite.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent ae)
{
txaList.setForeground(Color.white);
}
});
// Set initial message color to blue.
jrbWhite.setSelected(true);
txaList.setForeground(Color.white);
}
}