Friday 14 June 2013


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

class radio extends JFrame implements ActionListener
{
    JRadioButton red,blue,green;
    ButtonGroup color;
    Container c=getContentPane();

    radio()
    {
        //c=getContentPane();
        c.setLayout(null);
        red=new JRadioButton("red");
        red.addActionListener(this);
        red.setBounds(50,50,100,30);
        c.add(red);

        blue=new JRadioButton("blue");
        blue.addActionListener(this);
        blue.setBounds(50,100,100,30);
        c.add(blue);

        green=new JRadioButton("green");
        green.addActionListener(this);
        green.setBounds(50,150,100,30);
        c.add(green);


        color=new ButtonGroup();

        color.add(red);
        color.add(blue);
        color.add(green);

    }

    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();


        if(str=="red")
        {
            c.setBackground(Color.red);

        }

        if(str=="blue")
        {
            c.setBackground(Color.blue);
        }
        if(str=="green")
        {
            c.setBackground(Color.green);
        }
    }


 public static void main(String args[])
 {
     radio f=new radio();
     f.setTitle("My Frame");
     f.setSize(400,400);
     f.setVisible(true);
 }
}

No comments: