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

Task #1 Creating a GUI 1. Import the required Java libraries. 2. Create a class

ID: 3628027 • Letter: T

Question

Task #1 Creating a GUI
1. Import the required Java libraries.
2. Create a class called ColorFactory that inherits from JFrame.
3. Create named constants for a width of 500 and height of 300 for the frame.
4. Write a default constructor that does the following
a) Set the title of the window to Color Factory.
b) Set the size of the window using the constants.
c) Specify what happens when the close button is clicked.
d) Get the content pane of the JFrame and set the layout manager to border layout.
e) Call the method to build the top panel (to be written as directed below).
f) Add the panel to the north part of the content pane.
g) Call the method to build the bottom panel (to be written as directed below).
h) Add this panel to the south part of the content pane.
i) Create a label that contains the message “Top buttons change the panel color and bottom radio buttons change the text color.”
j) Add this label to the center part of the content pane.

Explanation / Answer

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

public class ColorFactory extends JFrame
{
//Fields of the class
private JLabel messageLabel; //A message to the user
private JPanel panelNorth; //A holding panel for the north border panel
private JPanel panelSouth; //A holding panel for the south border panel
private JPanel panelCenter; //A holding panel for the center border panel
private JButton redButton; //Changes panel color to red
private JButton orangeButton; //Changes panel color to orange
private JButton yellowButton; //Changes panel color to yellow
private JRadioButton greenRadioButton; //Changes text color to green
private JRadioButton blueRadioButton; //Changes text color to blue
private JRadioButton cyanRadioButton; //Changes text color to cyan
private final int WINDOW_WIDTH = 500; //Sets width of window
private final int WINDOW_HEIGHT = 300; //Sets height of window

/**
Constructor +ColorFactory()
*/
public ColorFactory()
{
//Set the title of the window to Color Factory.
setTitle("Color Factory");

//Set the size of the window using the constants 500 for window width and 300 for window height.
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

//Specify what happens when the close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Get the content pane of the JFrame and set the layout manager to border layout.
getContentPane().setLayout(new BorderLayout());

//Call the method buildNorthPanel to build the top panel.
buildNorthPanel();

//Add panelNorth to the north part of the content pane.
add(panelNorth, BorderLayout.NORTH);

//Call the method buildSouthPanel to build the bottom panel.
buildSouthPanel();

//Add panelSouth to the south part of the content pane.
add(panelSouth, BorderLayout.SOUTH);

//Create a label that contains the message “Top buttons change the panel color and
//bottom radio buttons change the text color.”
messageLabel = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");

//Add this label to the center part of the content pane.
add(panelCenter, BorderLayout.CENTER);

//Use setVisible method to display window on screen.
setVisible(true);

}//end constructor

/*
The buildNorthPanel method creates a panel that contains three buttons, red, orange, and yellow in the top panel.
*/
private void buildNorthPanel()
{
//Create three buttons, red, orange, and yellow.
redButton = new JButton("Red");
orangeButton = new JButton("Orange");
yellowButton = new JButton("Yellow");

//Use flow layout for the panel.
setLayout(new FlowLayout());

//Set the background to be white.
panelNorth.setBackground(Color.WHITE);

//Display red, orange, and yellow buttons in their respective colors.
redButton.setBackground(Color.RED);
orangeButton.setBackground(Color.ORANGE);
yellowButton.setBackground(Color.YELLOW);

//Add button listener that implements action listener for each button.
redButton.addActionListener(new ButtonListener());
orangeButton.addActionListener(new ButtonListener());
yellowButton.addActionListener(new ButtonListener());

}//end buildNorthPanel method

/*
The buildSouthPanel method creates a panel that contains three radio buttons, green, blue, and cyan in the
bottom panel.
*/
private void buildSouthPanel()
{
//Create three buttons, green, blue, and cyan.
greenRadioButton = new JRadioButton("Green");
blueRadioButton = new JRadioButton("Blue");
cyanRadioButton = new JRadioButton("Cyan");

//Use flow layout for the panel.
setLayout(new FlowLayout());

//Set the background to be white.
panelSouth.setBackground(Color.WHITE);

//Display green, blue, and cyan radio buttons in their respective colors.
greenRadioButton.setBackground(Color.GREEN);
blueRadioButton.setBackground(Color.BLUE);
cyanRadioButton.setBackground(Color.CYAN);

//Add button listener that implements action listener for each radio button.
greenRadioButton.addActionListener(new RadioButtonListener());
blueRadioButton.addActionListener(new RadioButtonListener());
cyanRadioButton.addActionListener(new RadioButtonListener());

}//end buildSouthPanel method

/*
Write a private inner class called ButtonListener that implements ActionListener. It
should contain an actionPerformed method to handle the button events.
*/
private class ButtonListener implements ActionListener
{
//actionPerformed method to handle all button events for red, orange, and yellow buttons.
public void actionPerformed(ActionEvent e)
{
//Determine which button was clicked to change color of center panel to respective color.
if (e.getSource() == redButton)
panelCenter.setBackground(Color.RED);
else if (e.getSource() == orangeButton)
panelCenter.setBackground(Color.ORANGE);
else if (e.getSource() == yellowButton)
panelCenter.setBackground(Color.YELLOW);

}//end actionPerformed method

}//end ButtonListener inner class

/*
Write a private inner class called RadioButtonListener that implements ActionListener. It
should contain an actionPerformed method to handle the radio button events.
*/
private class RadioButtonListener implements ActionListener
{
//actionPerformed method to handle all radio button events for green, blue, and cyan buttons.
public void actionPerformed(ActionEvent e)
{
//Determine which radio button was clicked to change color of text in center panel to respective color.
if (e.getSource() == greenRadioButton)
panelCenter.setForeground(Color.GREEN);
else if (e.getSource() == blueRadioButton)
panelCenter.setForeground(Color.ORANGE);
else if (e.getSource() == cyanRadioButton)
panelCenter.setForeground(Color.YELLOW);

}//end actionPerformed method

}//end RadioButtonListener inner class

}//end class

In a separate driver file, I have this:
Java Syntax (Toggle Plain Text)
public class Good_Brandon_Lab_08 //MUST match the file name!
{
public static void main (String[ ] args)
{
//Identify Programmer, Class, and Program Number
System.out.println("Program results by Brandon Good for CSCI 2911-H.");
System.out.println("This program solves Lab Week 8.");

//Create instance of ColorFactory.
ColorFactory colors = new ColorFactory();

}//end main method
}//end class

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