Exercise A: Write a GUI Application (ButtonColor) where user has 3 buttons to chose. Red, Blue, and Yellow. If user clicks on the Red button, background color of window changes to red, and the text color changes to white. If user clicks on the Blue button, background color of window changes to blue, and the text color changes to yellow. If user clicks on the Yellow button, background color of window changes to yellow, and the text color changes to black. Write action-listener for all 3 colors separately. Here is the sample output for clarity. Feel free to write more. Set the title of the window anyway you like. You clicked on BLUE color. JAVA
IN STEP 1 GIVE THE INTRODUCTION OF THE CONCEPT AND GIVE ANSWER FOR EACH PART OF THE QUESTION IN EACH DIFFERENT STEP WITH CLEAR EXPLANATION AND IN THE FINAL STEP GIVE THE WHOLE FINAL ANSWER IN JUST VERY FEW SENTENCES AND MOREOVER I NEED COMPLETE AND CLEAR ANSWER
at last explain what we did in each step in just few sentences££) My Favorit - 0 X | |&/MyFav.. — [m} X
Click a button to select a color.
Red || Bue || veliow Red | Blue | Yellow |
£|My Fav... — O X |£/My Fav... — Oo xX
You clicked on BLUE color. You clicked on YELLOW color.
[rea | sue | Red || sue || velow |
This is AI generated Response.
Asked by: Test User 100221
Created at: 2025-05-09 05:36:29
Answer:
RenderedRaw Text
Sure! Let's break down the task step by step, as you requested.
---
## **STEP 1: Introduction to the Concept**
This exercise demonstrates the basics of GUI (Graphical User Interface) programming in Java using Swing. Swing provides a set of "widgets" (like buttons, labels, and panels) that allow developers to create interactive windows. In this task, you will learn how to use buttons and action listeners to change the appearance of a window dynamically based on user input.
---
## **STEP 2: Creating the Window and Components**
- We need a main window (`JFrame`) with a label and three buttons: Red, Blue, and Yellow.
- The label displays instructions or the feedback message.
- The buttons allow the user to choose a color.
**Explanation**:
`JFrame` is the main window. `JLabel` shows text, and `JButton` allows users to click and interact.
---
## **STEP 3: Writing Action Listeners for Each Button**
- Each button needs its own `ActionListener`.
- **Red Button**: Changes background to red and text to white.
- **Blue Button**: Changes background to blue and text to yellow.
- **Yellow Button**: Changes background to yellow and text to black.
**Explanation**:
Action listeners detect when a button is clicked and run specific code to update the window's appearance.
---
## **STEP 4: Complete Java Code**
Below is the complete code, separated into logical sections for clarity.
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonColor extends JFrame {
private JLabel label;
private JPanel panel;
public ButtonColor() {
setTitle("My Favorite Color App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 250);
// Panel for layout and background
panel = new JPanel();
panel.setLayout(new FlowLayout());
label = new JLabel("Click a button to select a color.");
label.setFont(new Font("Arial", Font.BOLD, 18));
panel.add(label);
// Red Button
JButton redButton = new JButton("Red");
redButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.RED);
label.setForeground(Color.WHITE);
label.setText("You clicked on RED color.");
}
});
// Blue Button
JButton blueButton = new JButton("Blue");
blueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.BLUE);
label.setForeground(Color.YELLOW);
label.setText("You clicked on BLUE color.");
}
});
// Yellow Button
JButton yellowButton = new JButton("Yellow");
yellowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.YELLOW);
label.setForeground(Color.BLACK);
label.setText("You clicked on YELLOW color.");
}
});
// Add buttons to panel
panel.add(redButton);
panel.add(blueButton);
panel.add(yellowButton);
// Add panel to frame
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new ButtonColor();
}
}
```
---
## **STEP 5: Final Answer in Few Sentences**
We created a Java Swing GUI with three buttons (Red, Blue, Yellow). Each button changes the background and text color of the window, and updates the label message according to the selected color, providing interactive feedback to the user.
---
## **STEP 6: Summary of Each Step**
- **Step 1:** Introduced the concept of GUI programming and Swing.
- **Step 2:** Set up the main window and components (label, buttons).
- **Step 3:** Explained how to use action listeners for each button to change colors.
- **Step 4:** Provided the complete Java code for the application.
- **Step 5:** Summarized the solution in a few sentences.
---
**In this exercise, we learned how to build a user-interactive window using Java Swing, how to handle button clicks, and how to dynamically update both text and background colors in response to user actions.**