Question
// Can you check my work to see if it is correct? Thanks. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LeavinesCheckBoxes extends JFrame {
// Can you check my work to see if it is correct? Thanks.
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class LeavinesCheckBoxes extends JFrame { private JLabel messageLabel; // A message for the user private JCheckBox blueCheckBox; // To select blue background private JCheckBox orangeCheckBox; // To select orange foreground private final int WINDOW_WIDTH = 300; // Window width private final int WINDOW_HEIGHT = 100; // Window height public LeavinesCheckBoxes() { // Set the text for the title bar. setTitle("Color Check Boxes"); // Set the size of the window. setSize(WINDOW_WIDTH, WINDOW_HEIGHT); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a label. messageLabel = new JLabel("Select the check " + "boxes to change colors."); // Create the check boxes. blueCheckBox = new JCheckBox("Blue background"); orangeCheckBox = new JCheckBox("Red foreground"); // Add an item listener to the check boxes. blueCheckBox.addItemListener(new CheckBoxListener()); orangeCheckBox.addItemListener(new CheckBoxListener()); // Add a FlowLayout manager to the content pane. setLayout(new FlowLayout()); // Add the label and check boxes to the content pane. add(messageLabel); add(blueCheckBox); add(orangeCheckBox); // Display the window. setVisible(true); } private class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent e) { // Determine which check box was clicked. if (e.getSource() == blueCheckBox) { // Set the background color to blue. if (blueCheckBox.isSelected()) { // It was selected so therefore the background will appear blue. getContentPane().setBackground(Color.BLUE); blueCheckBox.setBackground(Color.BLUE); orangeCheckBox.setBackground(Color.BLUE); } else { // Blue check box was not selected. // Set the two check boxes to light gray. getContentPane().setBackground(Color.LIGHT_GRAY); blueCheckBox.setBackground(Color.LIGHT_GRAY); orangeCheckBox.setBackground(Color.LIGHT_GRAY); } } else if (e.getSource() == orangeCheckBox) { // Is orange box selected? // If so, set the foreground color to orange. if (orangeCheckBox.isSelected()) { // If the orange box is selected, set the foreground color // for the label and the two check boxes to orange. messageLabel.setForeground(Color.ORANGE); blueCheckBox.setForeground(Color.ORANGE); orangeCheckBox.setForeground(Color.ORANGE); } else { // The orange check box not selected. // Set the foreground color for the label // To white. messageLabel.setForeground(Color.WHITE); blueCheckBox.setForeground(Color.WHITE); orangeCheckBox.setForeground(Color.WHITE); } } } } public static void main(String[] args) { new LeavinesCheckBoxes(); } }
Write a program with two checkboxes, one labeled BLUE and the other labeled ORANGE. When no checkbox is selected, the panel's background color should be gray and its foreground color should be black. When only the BLUE checkbox is selected, the panel's background color should be blue and its foreground color should be yellow. When only the ORANGE checkbox is selected, the panel's background color should be orange and its foreground color should be white. When both checkboxes are selected, the panel's background color should be blue and its foreground color should be orange. Please note that checkboxes require the use of the ItemListener, as opposed to the ActionListener
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started