In: Computer Science
Write an application with three radio buttons labeled “Cyan”, “Magenta”, and “Orange” that changes the background color of a panel in the center of the frame to CYAN, MAGENTA, or ORANGE.
This is required to be programmed in Java.
import javax.swing.*; // package
import java.awt.*;
import java.awt.event.*;
class ColorMain extends Frame implements ActionListener{
private JRadioButton Cyan;
private JRadioButton Magenta;
private JRadioButton Orange; // Decalare radio button
public ColorMain(){
setLayout(new BorderLayout());
setVisible(true);
setSize(400,250);
JPanel p = new JPanel();
Cyan=new JRadioButton(" Cyan");
Orange=new JRadioButton(" Orange");
Magenta=new JRadioButton(" magenta");
ButtonGroup bgroup=new ButtonGroup();
bgroup.add(Cyan);
bgroup.add(Orange);
bgroup.add(Magenta);
Cyan.addActionListener(this);
Orange.addActionListener(this);
Magenta.addActionListener(this);
p.add(Orange); // place the light component in a saparate container.
p.add(Cyan);
p.add(Magenta);
add(p,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent evnt){ //set bg color when button clicked
if(evnt.getSource()== Cyan){
setBackground(Color.cyan);
}
if(evnt.getSource()== Orange){
setBackground(Color.orange);
}
if(evnt.getSource()== Magenta){
setBackground(Color.magenta);
}
}
}
//driver class
public class Colorbg {
public static void main(String[] args){
ColorMain s=new ColorMain();
s.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}