simo1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class simo2 extends JFrame implements ActionListener{
private JRadioButton red;
private JRadioButton yellow;
private JRadioButton blue;
private JRadioButton green;
private JRadioButton magenta;
public simo2()
{
setLayout(new FlowLayout());
setVisible(true);
setSize(400,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
red=new JRadioButton(" red");
yellow=new JRadioButton(" yellow");
blue=new JRadioButton(" blue");
green=new JRadioButton(" green");
magenta=new JRadioButton(" magenta");
ButtonGroup group=new ButtonGroup();
group.add(red);
group.add(yellow);
group.add(blue);
group.add(green);
group.add(magenta);
red.addActionListener(this);
yellow.addActionListener(this);
blue.addActionListener(this);
green.addActionListener(this);
magenta.addActionListener(this);
add(red);
add(yellow);
add(blue);
add(green);
add(magenta);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== red)
{
setBackground(Color.red);
}
if(e.getSource()== yellow)
{
setBackground(Color.yellow);
}
if(e.getSource()== blue)
{
setBackground(Color.blue);
}
if(e.getSource()== green)
{
setBackground(Color.green);
}
if(e.getSource()== magenta)
{
setBackground(Color.magenta);
}
}
}
In simo1.java
public class simo1 {
public static void main(String[] args)
{
simo2 s=new simo2();
}
}
Everything is compiling fine and in output radio button is displayed well with all color text displayed in it but when clicking red, no background color is displayed and also for all similar colors. Can't get background color when clicking selective button??