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

Within java Add the check boxes that changes color to the panels to the code bel

ID: 654203 • Letter: W

Question

Within java

Add the check boxes that changes color to the panels to the code below.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class GUI5 extends JFrame{

       private Container contentPane;

       private JPanel panel1, panel2;

       private JButton button1;

       private JTextField text1;

       private JRadioButton radio1;

       private JRadioButton radio2;

       private ButtonGroup group;

       private ActionListener listener;

       private int count;

       public GUI5(){

              setTitle("GUI5");

              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              panel1 = new JPanel();

              panel1.setBackground(Color.GREEN);

              panel1.setPreferredSize(new Dimension (300,100));

              listener = new Listener();

              button1 = new JButton("Click to count");

              button1.addActionListener(listener);

              panel1.add(button1);

              radio1 = new JRadioButton("Up", true);

              radio2 = new JRadioButton("Down");

              group = new ButtonGroup();

              group.add(radio1);

              group.add(radio2);

              panel1.add(radio1);

              panel1.add(radio2);

              panel2= new JPanel();

              panel2.setBackground(Color.yellow);

              panel2.setPreferredSize(new Dimension(200,100));

              text1 = new JTextField(10);

              count=0;

              text1.setText(""+count);

              text1.addActionListener(listener);

              panel2.add(text1);

              contentPane= getContentPane();

              contentPane.setLayout(new FlowLayout());

              contentPane.add(panel1);

              contentPane.add(panel2);

              pack();

              setVisible(true);

       }

       public static void main(String[] args){

              GUI5 window = new GUI5();

       }

       private class Listener implements ActionListener{

              public void actionPerformed(ActionEvent e){

                     if (e.getSource() == text1)

                           count = Integer.parseInt(text1.getText());

                     else

                           if (e.getSource()==button1)

                                  if(radio1.isSelected())

                                         text1.setText(""+ ++count);

                                  else

                                         if (radio2.isSelected())

                                                text1.setText("" + --count);

              }

       }

}

Radio buttons are used to specify mutually exclusive options. Check boxes are similar to radio buttons, but are used to specify options that not mutually exclusive. Any combination of check boxes can be checked at one time. A check box is created from the JCheckBox class. Add two check boxes to the Cl8h5. java (acopy of GUI5 in Fig. 18.7) program, one on each panel, that control the background color of its panel. When a checkbox on a panel is checked, the background color on that panel should go white. When unchecked, the background color should revert to its original color (yellow for panel1 or green for panel2).

Explanation / Answer

the condition wich is used for radio buttons can be used for the check boxes if true than set bg color is white other wise take the bsic color of panel 1 or 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote