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

Create the program within java import javax.swing.*; import java.awt.*; import j

ID: 653873 • Letter: C

Question

Create the program within java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI5 extends JFrame
{
private Container contentPane;
private JPanel panel1, panel2;
private JButton button;
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(); // create panel
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); // create button
radio2 = new JRadioButton("Down"); // create button
group = new ButtonGroup(); // create button group
group.add(radio1); // add radio buttons to group
group.add(radio2);
panel1.add(radio1); // add radio button to panel
panel1.add(radio2); // add radio button to panel

panel2 = new JPanel(); // create panel
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);

// get content pane of frame
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(panel1); // add panels to content pane
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()) // radio button 1 selected?
text1.setText("" + ++count); // increment count
else
if (radio2.isSelected()) // radio button 2 selected?
text1.setText("" + --count); // decrement 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 C18h5. java (a copyof GUI5in 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

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI5 extends JFrame
{
    private Container contentPane;
    private JPanel panel1, panel2;
    private JButton button;
    private JTextField text1;
    private JRadioButton radio1;
    private JRadioButton radio2;
    private ButtonGroup group;
    private ActionListener listener;
    private int count;
    //----------------------------------

JCheckBox checkcolor;
JCheckBox checkcolor1;

//-------------
   public GUI5()
    {
       setTitle("GUI5");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      panel1 = new JPanel();           // create panel
       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); // create button
       radio2 = new JRadioButton("Down");      // create button
       group = new ButtonGroup(); // create button group
       group.add(radio1);          // add radio buttons to group
       group.add(radio2);
       panel1.add(radio1);         // add radio button to panel
       panel1.add(radio2);         // add radio button to panel

      panel2 = new JPanel();      // create panel
       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);

checkcolor1 = new JCheckBox("checkcolor1");
        checkcolor1.setSelected(false);
        checkcolor1.addItemListener(listener);
panel1.add(Checkcolor1);

Checkcolor = new JCheckBox("Checkcolor");
        Checkcolor.setSelected(false);
        Checkcolor.addItemListener(listener);
panel2.add(Checkcolor);

      // get content pane of frame
       contentPane = getContentPane();
       contentPane.setLayout(new FlowLayout());
       contentPane.add(panel1);    // add panels to content pane
       contentPane.add(panel2);

      pack();
       setVisible(true);
    }
    //----------------------------------
   public static void main(String[] args)
    {
       GUI5 window = new GUI5();
    }


class listener implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
             Object source = e.getSource();
            if (source == checkcolor1) {
if (e.getStateChange() == ItemEvent.SELECTED)
                panel1.setBackground(Color.WHITE)
}
if(source ==checkcolor2){

            if (e.getStateChange() == ItemEvent.SELECTED)
                panel2.setBackground(Color.WHITE);
}
           
        }
    }


    //----------------------------------
   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()) // radio button 1 selected?
                text1.setText("" + ++count); // increment count
             else
             if (radio2.isSelected()) // radio button 2 selected?
                text1.setText("" + --count); // decrement count
       }
    }
}