File ColorOptions.java contains a program that will display a set of radio butto
ID: 3659600 • Letter: F
Question
File
ColorOptions.java contains a program that will display a set of radio buttons that let the user change the background color of the GUI. The file ColorOptionsPanel.java contains the skeleton of the panel for this program. Open the files and study the code that is already there. You will note that in ColorOptionsPanel.java there is an array color containing 5 colors already defined. Your task is to add an array of radio buttons so that a click of a radio button will cause the background of the panel to change to the corresponding color in the color array.
1. Define
colorButton to be an array of NUM_COLORS objects of type JRadioButton.
2. Instantiate each
colorButton with the appropriate color as the label (for example, the first button should be labeled "Yellow"). The first button (corresponding to yellow) should be on (true) initially.
3. Recall that radio buttons must be grouped and that the selection of a radio button produces an action event. Hence you must have a ButtonGroup object and an ActionListener. Note that the skeleton of an ActionListener named
ColorListener is already provided. So, you need to:
a. Instantiate a ButtonGroup object and a ColorListener object. Comments in the code indicate where to do this.
b. Each radio button needs to be added to your ButtonGroup object, the background color needs to be set (use white), your ColorListener needs to be added, and the button needs to be added to the panel. All of these can be done using a single for loop. So, add a for loop that goes through the radio buttons adding each to your ButtonGroup object, setting the background of each to white, adding your ColorListener to each, and adding each to the panel.
4. Fill in the body of the actionPerformed method. This method needs to go through the buttons to determine which is selected and then set the background color accordingly. A simple for loop can do this. Use the
isSelected method to determine if a button is selected (for example, if (colorButton[i].isSelected())....). Use the color array to set the background color.
5. Test your program!
// ********************************************************************
// ColorOptions.java
//
// Uses an array of radio buttons to change the background color. 5
// ********************************************************************
import javax.swing.*;
public class ColorOptions
{
// -----------------------------------------------------------
// Creates and presents the frame for the color change panel.
// -----------------------------------------------------------
public static void main (String[] args)
{
JFrame colorFrame = new JFrame ("Color Options");
colorFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ColorOptionsPanel panel = new ColorOptionsPanel();
colorFrame.getContentPane().add (panel);
colorFrame.pack();
colorFrame.setVisible(true);
}
}
// ***********************************************************************
// ColorOptionsPanel.java
//
// Represents the user interface for the ColorOptions program that lets
// the user change background color by selecting a radio button.
// ***********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorOptionsPanel extends JPanel
{
private final int WIDTH = 350, HEIGHT = 100, FONT_SIZE = 20;
private final int NUM_COLORS = 5;
private Color [] color = new Color[NUM_COLORS];
private JLabel heading;
// ------------------------------------------------------------------
// Sets up a panel with a label at the top and a set of radio buttons
// that control the background color of the panel.
// ------------------------------------------------------------------
public ColorOptionsPanel ()
{
// Set up heading and colors
heading = new JLabel ("Choose the background color!");
heading.setFont (new Font ("Helvetica", Font.BOLD, FONT_SIZE));
color[0] = Color.yellow;
color[1] = Color.cyan;
color[2] = Color.red;
color[3] = Color.green;
color[4] = Color.magenta;
// Instantiate a ButtonGroup object and a ColorListener object
// Set up the panel
add (heading);
setBackground (Color.yellow);
setPreferredSize (new Dimension (WIDTH, HEIGHT));
// Group the radio buttons, add a ColorListener to each,
// set the background color of each and add each to the panel.
}
// ***************************************************************
// Represents the listener for the radio buttons. 6
// ***************************************************************
private class ColorListener implements ActionListener
{
// -------------------------------------------------------
// Updates the background color of the panel based on
// which radio button is selected.
// ------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
}
}
}
Explanation / Answer
ColorOptionsPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ColorOptionsPanel extends JPanel
{
private final int WIDTH = 350, HEIGHT = 100, FONT_SIZE = 20;
private final int NUM_COLORS = 5;
private Color[] color = new Color[NUM_COLORS];
private JLabel heading;
JRadioButton colorButtons[];
// ------------------------------------------------------------------
// Sets up a panel with a label at the top and a set of radio buttons
// that control the background color of the panel.
// ------------------------------------------------------------------
public ColorOptionsPanel()
{
// Set up heading and colors
heading = new JLabel("Choose the background color!");
heading.setFont(new Font("Helvetica", Font.BOLD, FONT_SIZE));
color[0] = Color.yellow;
color[1] = Color.cyan;
color[2] = Color.red;
color[3] = Color.green;
color[4] = Color.magenta;
colorButtons = new JRadioButton[color.length];
colorButtons[0] = new JRadioButton("Yellow");
colorButtons[1] = new JRadioButton("Cyan");
colorButtons[2] = new JRadioButton("Red");
colorButtons[3] = new JRadioButton("Green");
colorButtons[4] = new JRadioButton("Magneta");
ButtonGroup group = new ButtonGroup();
group.add(colorButtons[0]);
group.add(colorButtons[1]);
group.add(colorButtons[2]);
group.add(colorButtons[3]);
group.add(colorButtons[4]);
//Register a listener for the radio buttons.
colorButtons[0].addActionListener(new ColorListener());
colorButtons[1].addActionListener(new ColorListener());
colorButtons[2].addActionListener(new ColorListener());
colorButtons[3].addActionListener(new ColorListener());
colorButtons[4].addActionListener(new ColorListener());
// Instantiate a ButtonGroup object and a ColorListener object
// Set up the panel
add(heading);
add(colorButtons[0]);
add(colorButtons[1]);
add(colorButtons[2]);
add(colorButtons[3]);
add(colorButtons[4]);
setBackground(Color.yellow);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
// Group the radio buttons, add a ColorListener to each,
// set the background color of each and add each to the panel.
}
// ***************************************************************
// Represents the listener for the radio buttons. 6
// ***************************************************************
private class ColorListener implements ActionListener
{
// -------------------------------------------------------
// Updates the background color of the panel based on
// which radio button is selected.
// ------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("Red"))
setBackground(Color.red);
if(event.getActionCommand().equals("Yellow"))
setBackground(Color.yellow);
if(event.getActionCommand().equals("Green"))
setBackground(Color.green);
if(event.getActionCommand().equals("Cyan"))
setBackground(Color.cyan);
if(event.getActionCommand().equals("Magneta"))
setBackground(Color.magenta);
}
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.