Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CHECK BOXES PROJECT LANGUAGE: JAVA Write a program with two checkboxes, one labe

ID: 3803818 • Letter: C

Question

CHECK BOXES PROJECT

LANGUAGE: JAVA

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.

Remember to include comments!

Check Boxes R Criteria Correct Project Name Check Box Output Correct Blue Orange Check Box utput Correct Both Check Boxes Select Output Correct Ratings Pts 2.0 pts 6.0 pts 6.0 pts 6.0 pts Total Points: 2

Explanation / Answer


import java.awt.*;
import java.awt.event.*;

public class CheckBox {
private Frame mFrame;
private Panel cPanel;
private Label hLabel;
private Label sLabel;

public CheckBox(){
initGUI();
}

public static void main(String[] args){
CheckBox check = new CheckBox();
check.display();
}

private void initGUI(){
   mFrame = new Frame("My Java Frame");
   mFrame.setSize(500,500);
   mFrame.setLayout(new GridLayout(7, 2));
   mFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});   
  
   hLabel = new Label();
   hLabel.setAlignment(Label.CENTER);
   sLabel = new Label();
   sLabel.setAlignment(Label.CENTER);
   sLabel.setSize(450,150);

   cPanel = new Panel();
   cPanel.setLayout(new FlowLayout());
   cPanel.setBackground(Color.gray);
   cPanel.setForeground(Color.black);
mFrame.add(hLabel);
mFrame.add(cPanel);
mFrame.add(sLabel);
mFrame.setVisible(true);
}

private void display(){

Checkbox blue = new Checkbox("BLUE");
Checkbox orange = new Checkbox("ORANGE");

blue.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {   
if(e.getStateChange()==1)
{
   cPanel.setBackground(Color.blue);
   cPanel.setForeground(Color.yellow);
}
else
{
   cPanel.setBackground(Color.gray);
   cPanel.setForeground(Color.black);
}
}
});

orange.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1)
{
   cPanel.setBackground(Color.orange);
   cPanel.setForeground(Color.white);
}
else
{
   cPanel.setBackground(Color.gray);
   cPanel.setForeground(Color.black);
}
}
});
cPanel.add(blue);
cPanel.add(orange);
mFrame.setVisible(true);
}
}