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

6. Make the following changes to the program. a. Implement the ActionListener in

ID: 662196 • Letter: 6

Question

6. Make the following changes to the program.
a. Implement the ActionListener in the class header.
b. Change the names of the Buttons to Red,Yellow, Cyan, Magenta, and
White.Add the ActionListener to each Button.
c. Below the main() method but still within the class block, enter an
actionPerformed() method using the header, public void
actionPerformed(ActionEvent e) and an opening brace.
d. Declare and assign a variable, arg, by typing, String arg =
e.getActionCommand(); within the block.
e. Test for the click of each button by writing an if statement similar to the
following: if (arg == "Yellow"). The result of a button click should
be a change in the background color of the application.
7. Save the source code. Compile, correct any errors, and then run the applet.
Print a copy of the source code.
8. Finally, make the following changes to the program:
a. Delete the button in the center area.Replace it with a Choice component
named colors.
b. Use the add() method to populate the Choice component with the colors
from step 6b above.
c. At the end of the Buttons class header, insert a comma, and then type
ItemListener to implement the ItemListener.
d. Add an itemListener to the Choice component and then add the Choice
component to the center area of the BorderLayout.
e. Write an itemStateChanged() method to test for each item in the
Choice component using the header, public void itemStateChanged
(ItemEvent ie), and an opening brace. Use the same code as you did
in the actionPerformed() method to test for the click of each choice
(step 6e above).

/*
   Chapter 6:   Buttons

   Filename:   Buttons.java
   Purpose:
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Buttons extends Frame implements ActionListener
{
   public Buttons()
   {
       //set the layout
       setLayout(new BorderLayout(20, 5));


         //Add buttons
         Button Red = new Button("Red");
         Button Yellow = new Button("Yellow");
         Button Cyan = new Button("Cyan");
         Button Magenta = new Button("Magenta");
         Button White = new Button("White");
       add(Red, BorderLayout.NORTH);
       add(Yellow, BorderLayout.SOUTH);
       add(Cyan, BorderLayout.EAST);
       add(Magenta, BorderLayout.WEST);
       add(White, BorderLayout.CENTER);


       //override the windowClosing event
       addWindowListener(
           new WindowAdapter()
               {
               public void windowClosing(WindowEvent e)
                   {
                       System.exit(0);
                   }
               }
       );

   }

    public static void main(String[] args)
    {
          // set frame properties
       Buttons f = new Buttons();
         f.setTitle("Border Application");
         f.setBackground(Color.red);
         f.setBounds(200,200,300,300);
        f.setVisible(true);

   }
   public void actionPerformed(ActionEvent e){
       String arg = e.getActionCommand();
       if(arg == "Yellow")
              frame.setBackground(Color.yellow);

   }
}

Explanation / Answer

Code for Answer 6 to 7

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

import javax.swing.*;

public class Buttons extends Frame implements ActionListener
{
   static Buttons f;
   public Buttons()
   {
       //set the layout
       setLayout(new BorderLayout(20, 5));


         //Add buttons
         Button Red = new Button("Red");
         Button Yellow = new Button("Yellow");
         Button Cyan = new Button("Cyan");
         Button Magenta = new Button("Magenta");
         Button White = new Button("White");
       add(Red, BorderLayout.NORTH);
       add(Yellow, BorderLayout.SOUTH);
       add(Cyan, BorderLayout.EAST);
       add(Magenta, BorderLayout.WEST);
       add(White, BorderLayout.CENTER);
     
       Red.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           Buttons.this.actionPerformed(e);
       }          
       });
       Yellow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
       Cyan.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
       Magenta.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
       White.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });


       //override the windowClosing event
       addWindowListener(
           new WindowAdapter()
               {
               public void windowClosing(WindowEvent e)
                   {
                       System.exit(0);
                   }
               }
       );

   }

    public static void main(String[] args)
    {
          // set frame properties
       f = new Buttons();
         f.setTitle("Border Application");
         f.setBackground(Color.red);
         f.setBounds(200,200,300,300);
        f.setVisible(true);

   }
   public void actionPerformed(ActionEvent e){
       String arg = e.getActionCommand();
       if(arg == "Red")
              f.setBackground(Color.red);
       else if(arg == "Yellow")
           f.setBackground(Color.yellow);
       else if(arg == "Cyan")
           f.setBackground(Color.cyan);
       else if(arg == "Magenta")
           f.setBackground(Color.magenta);
       else if(arg == "White")
           f.setBackground(Color.white);

   }
}

Code for answer 8 a to e

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

import javax.swing.*;

public class Buttons extends Frame implements ActionListener,ItemListener
{
   static Buttons f;
   public Buttons()
   {
       //set the layout
       setLayout(new BorderLayout(20, 5));


         //Add buttons
         Button Red = new Button("Red");
         Button Yellow = new Button("Yellow");
         Button Cyan = new Button("Cyan");
         Button Magenta = new Button("Magenta");
         Choice colors = new Choice();
       
       add(Red, BorderLayout.NORTH);
       add(Yellow, BorderLayout.SOUTH);
       add(Cyan, BorderLayout.EAST);
       add(Magenta, BorderLayout.WEST);
       add(colors, BorderLayout.CENTER);
     
       colors.add("Red");
       colors.add("Yellow");
       colors.add("Cyan");
       colors.add("Magenta");
       colors.add("White");
     
       Red.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           Buttons.this.actionPerformed(e);
       }          
       });
       Yellow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
       Cyan.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
       Magenta.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Buttons.this.actionPerformed(e);
        }          
          });
     
       colors.addItemListener(this);
     
       //override the windowClosing event
       addWindowListener(
           new WindowAdapter()
               {
               public void windowClosing(WindowEvent e)
                   {
                       System.exit(0);
                   }
               }
       );

   }

   public void itemStateChanged(ItemEvent ie)
         {
                    String arg = ie.getItem().toString();

                     if (arg == "Red")

                         setBackground(Color.red);

   

                     if (arg == "Yellow")

                         setBackground(Color.yellow);

   

                     if (arg == "Cyan")

                         setBackground(Color.cyan);

   

                     if (arg == "Magenta")

                         setBackground(Color.magenta);

   

                     if (arg == "White")

                 setBackground(Color.white);

         }

    public static void main(String[] args)
    {
          // set frame properties
       f = new Buttons();
         f.setTitle("Border Application");
         f.setBackground(Color.red);
         f.setBounds(200,200,300,300);
        f.setVisible(true);

   }
   public void actionPerformed(ActionEvent e){
       String arg = e.getActionCommand();
       if(arg == "Red")
              f.setBackground(Color.red);
       else if(arg == "Yellow")
           f.setBackground(Color.yellow);
       else if(arg == "Cyan")
           f.setBackground(Color.cyan);
       else if(arg == "Magenta")
           f.setBackground(Color.magenta);
       else if(arg == "White")
           f.setBackground(Color.white);

   }
}

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