Java Reference
In-Depth Information
be selected, once you know the source of the event, you can conclude that the associated
radio button is selected. Thus, the relevant code for handling the events generated by
these radio buttons can be written as follows:
if (e.getSource() == redRB)
currentColor = Color.red;
else if (e.getSource() == greenRB)
currentColor = Color.green;
else if (e.getSource() == blueRB)
currentColor = Color.blue;
The complete program, along with the sample run, follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GrandWelcomeRButton extends JApplet implements
ItemListener
{
private int intBold = Font.PLAIN;
private int intItalic = Font.PLAIN;
private Color currentColor = Color.black;
private JCheckBox boldCB, italicCB;
private JRadioButton redRB, greenRB, blueRB;
private ButtonGroup ColorSelectBGroup;
public void init()
{
Container c = getContentPane();
c.setLayout( null );
boldCB = new JCheckBox("Bold");
italicCB = new JCheckBox("Italic");
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");
boldCB.setSize(100, 30);
italicCB.setSize(100, 30);
redRB.setSize(100, 30);
greenRB.setSize(100, 30);
blueRB.setSize(100, 30);
boldCB.setLocation(100, 70);
italicCB.setLocation(100, 150);
redRB.setLocation(300, 70);
greenRB.setLocation(300, 110);
blueRB.setLocation(300, 150);
boldCB.addItemListener( this );
italicCB.addItemListener( this );
redRB.addItemListener( this );
Search WWH ::




Custom Search