I want to use a 'Clear' button to clear the background color of a JFrame after using a 'color' button to add color to the background. Everything I've been able to find tells me how to change the value to a different color but not how to remove it.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorFrame extends JFrame {
JButton red, green, blue, clear;
public ColorFrame() {
super("ColorFrame");
setSize(322, 122);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
red = new JButton ("Red");
add(red);
green = new JButton ("Green");
add(green);
blue = new JButton("Blue");
add(blue);
clear = new JButton("Clear");
add(clear);
ActionListener act = new ActionListener() {
public void actionPerformed (ActionEvent event) {
if(event.getSource() == red) {
getContentPane().setBackground(Color.RED);
}
if(event.getSource() == green) {
getContentPane().setBackground(Color.GREEN);
}
if(event.getSource() == blue) {
getContentPane().setBackground(Color.BLUE);
}
if(event.getSource() == clear) {
//clear background color
}
}
};
red.addActionListener(act);
green.addActionListener(act);
blue.addActionListener(act);
clear.addActionListener(act);
setVisible(true);
}
public static void main(String arguments[]) {
new ColorFrame();
}
}