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

we will be creating a graphical user interface (GUI) to allow the user to select

ID: 3657961 • Letter: W

Question

we will be creating a graphical user interface (GUI) to allow the user to select a button that will change the color of the center panel and radio buttons that will change the color of the text in the center panel. We will need to use a variety of Swing components to accomplish this task. 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

Explanation / Answer

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





public class ColorFactory extends JFrame
{
private JPanel TopPanel;
private JPanel BottomPanel;
private JPanel CenterPanel;
private JLabel messageLabel;
private JButton red;
private JButton orange;
private JButton yellow;
private ButtonGroup radioButtonGroup;
private JRadioButton green;
private JRadioButton blue;
private JRadioButton cyan;

private final int WINDOW_WIDTH = 600;
private final int WINDOW_HEIGHT = 400;

/**
constructor
*/

public ColorFactory()
{

// calls the super constructor
super("Color Factory");

//Label message
messageLabel = new JLabel("Top Buttons Change The Panel Color And" +
"Bottom Radio Buttons Change The Text Color.");

//set size of window
setSize (WINDOW_WIDTH, WINDOW_HEIGHT);

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

//set the layout
setLayout(new BorderLayout());

//creat panels
JPanel TopPanel = new JPanel();
JPanel BottomPanel = new JPanel();
JPanel CenterPanel = new JPanel();


//method to build panels
buildTopPanel();
buildBottomPanel();

//add panel
add(CenterPanel, BorderLayout.CENTER);

//Add Labels
CenterPanel.add(messageLabel);



setVisible(true);
}


private void buildTopPanel()
{

JPanel TopPanel=new JPanel();

add(TopPanel, BorderLayout.NORTH);

TopPanel.setBackground(Color.WHITE);

//creat buttons
JButton red=new JButton("Red");
JButton orange=new JButton("Orange");
JButton yellow=new JButton("Yellow");

//set background
red.setBackground(Color.RED);
orange.setBackground(Color.ORANGE);
yellow.setBackground(Color.YELLOW);

//Action listener
red.addActionListener(new ButtonListener());
orange.addActionListener(new ButtonListener());
yellow.addActionListener(new ButtonListener());

//add buttons to panel
TopPanel.add(red);
TopPanel.add(orange);
TopPanel.add(yellow);

}


private void buildBottomPanel()
{


JPanel BottomPanel=new JPanel();

add(BottomPanel, BorderLayout.SOUTH);


//set color
BottomPanel.setBackground(Color.WHITE);

//creat buttons
green=new JRadioButton("Green");
blue=new JRadioButton("Blue");
cyan=new JRadioButton("Cyan");

//set foreground
green.setForeground(Color.GREEN);
blue.setForeground(Color.BLUE);
cyan.setForeground(Color.CYAN);

//set button grouping
radioButtonGroup=new ButtonGroup();
radioButtonGroup.add(green);
radioButtonGroup.add(blue);
radioButtonGroup.add(cyan);

// action listener
green.addActionListener(new RadioButtonListener());
blue.addActionListener(new RadioButtonListener());
cyan.addActionListener(new RadioButtonListener());

//add buttons to panel
BottomPanel.add(green);
BottomPanel.add(blue);
BottomPanel.add(cyan);
}

//Task 3

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

if(e.getSource() == red)
{
CenterPanel.setBackground(Color.RED);
}

else if(e.getSource() == orange)
{
CenterPanel.setBackground(Color.ORANGE);
}

else if(e.getSource() == yellow)
{
CenterPanel.setBackground(Color.YELLOW);
}
}
}

private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==green)
{
messageLabel.setForeground(Color.GREEN);
}

else if (e.getSource()==blue)
{
messageLabel.setForeground(Color.BLUE);
}

else if (e.getSource()==cyan)
{
messageLabel.setForeground(Color.CYAN);
}
}
}

//task4

public static void main(String[] args)
{
new ColorFactory();
}


}