Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program with two checkboxes, one labeled BLUE and the other labeled ORANGE. When no checkbox is selected, the panels background color should be

Write a program with two checkboxes, one labeled BLUE and the other labeled ORANGE. When no checkbox is selected, the panels background color should be gray and its foreground color should be black. When only the BLUE checkbox is selected, the panels background color should be blue and its foreground color should be yellow. When only the ORANGE checkbox is selected, the panels background color should be orange and its foreground color should be white. When both checkboxes are selected, the panels background color should be blue and its foreground color should be orange.

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 = 500; // Window width private final int WINDOW_HEIGHT = 300; // 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("Click the check boxes."); // Create the check boxes. blueCheckBox = new JCheckBox("BLUE"); orangeCheckBox = new JCheckBox("ORANGE"); // 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 GridLayout(1,2)); // 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); // It was selected so therefore the foreground will appear yellow. getContentPane().setForeground(Color.yellow); blueCheckBox.setForeground(Color.yellow); orangeCheckBox.setForeground(Color.yellow); } else { // Blue check box was not selected. // Set the two check boxes to light gray. getContentPane().setBackground(Color.gray); blueCheckBox.setBackground(Color.gray); orangeCheckBox.setBackground(Color.gray); getContentPane().setForeground(Color.black); blueCheckBox.setForeground(Color.black); orangeCheckBox.setForeground(Color.black); } } else if (e.getSource() == orangeCheckBox) { // Is orange box selected? // If so, set the foreground color to orange. if (orangeCheckBox.isSelected()) { if (blueCheckBox.isSelected()) { getContentPane().setBackground(Color.blue); blueCheckBox.setBackground(Color.blue); orangeCheckBox.setBackground(Color.blue); getContentPane().setForeground(Color.orange); blueCheckBox.setForeground(Color.orange); orangeCheckBox.setForeground(Color.orange); } getContentPane().setBackground(Color.orange); blueCheckBox.setBackground(Color.orange); orangeCheckBox.setBackground(Color.orange); getContentPane().setForeground(Color.white); blueCheckBox.setForeground(Color.white); orangeCheckBox.setForeground(Color.white); } else { blueCheckBox.setForeground(Color.black); orangeCheckBox.setForeground(Color.black); getContentPane().setForeground(Color.black); blueCheckBox.setForeground(Color.black); orangeCheckBox.setForeground(Color.black); } } } } public static void main(String[] args) { new LeavinesCheckBoxes(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Is this investment worthwhile? Why or why not?

Answered: 1 week ago