Alter the GUI Java code such that each of the buttons on the GUI has two functio
ID: 3534978 • Letter: A
Question
Alter the GUI Java code such that each of the buttons on the GUI has two functions, one to change the color, and one to reset it to its original look & feel, the button should toggle back and forth each time the button is clicked
javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GuiDemo
{
JPanel panel;
GuiDemo()
{
JLabel redLabel = new JLabel("Click to change color");
JLabel blueLabel =
new JLabel("Click to change color");
JLabel backLabel =
new JLabel("Click to change background color");
final JButton redButton = new JButton("Red");
redButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
redButton.setBackground(Color.RED)…
}
});
final JButton blueButton = new JButton("Blue");
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
blueButton.setBackground(Color.BLU…
}
});
final JButton backButton = new JButton("Background");
backButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel.setBackground(Color.GREEN);
}
});
panel = new JPanel();
panel.add(redLabel);
panel.add(redButton);
panel.add(blueLabel);
panel.add(blueButton);
panel.add(backLabel);
panel.add(backButton);
}
public static void main(String args[])
{
GuiDemo demo = new GuiDemo();
JFrame frame = new JFrame("GUI Demo");
frame.setDefaultCloseOperation(JFram…
frame.setContentPane(demo.panel);
frame.pack();
frame.setVisible(true);
}
Explanation / Answer
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class GuiDemo {
JPanel panel;
boolean red;
boolean blue;
boolean green;
Color color;
GuiDemo() {
red = false;
blue = false;
green = false;
JLabel redLabel = new JLabel("Click to change color");
JLabel blueLabel = new JLabel("Click to change color");
JLabel backLabel = new JLabel("Click to change background color");
final JButton redButton = new JButton("Red");
redButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!red) {
color = redButton.getBackground();
redButton.setBackground(Color.RED);
red = true;
}
else {
redButton.setBackground(color);
red = false;
}
}
});
final JButton blueButton = new JButton("Blue");
blueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!blue) {
color = blueButton.getBackground();
blueButton.setBackground(Color.BLUE);
blue = true;
}
else {
blueButton.setBackground(color);
blue = false;
}
}
});
final JButton backButton = new JButton("Background");
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!green) {
color = panel.getBackground();
panel.setBackground(Color.GREEN);
green = true; }
else {
panel.setBackground(color);
green = false;
}
}
});
panel = new JPanel();
panel.add(redLabel);
panel.add(redButton);
panel.add(blueLabel);
panel.add(blueButton);
panel.add(backLabel);
panel.add(backButton);
}
public static void main(String args[]) {
GuiDemo demo = new GuiDemo();
JFrame frame = new JFrame("GUI Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(demo.panel);
frame.pack();
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.