Package Button;import java.awt.*;import java.awt.event.*;import javax.swing.*;p ublic class Buttonframe extends JFrame{ Private JPanel buttonpanel;private static final int d_width=300;private static final int d_height=200;public static void M Ain (String args[]) {JFrame frame = new Buttonframe (); Frame.settitle ("Buttonframe"); Frame.setdefaultcloseoperation ( Jframe.exit_on_close); frame.setvisible (true);} Public Buttonframe () {setSize (d_width,d_height); JButton Yellowbutton = new JButton ("Yellow"); JButton BlueButton = new JButton ("Blue"); JButton Redbutton = new JButton ("Red") Buttonpanel = new JPanel (); Buttonpanel.add (Yellowbutton); Buttonpanel.add ( BlueButton); Buttonpanel.add (Redbutton); add (Buttonpanel); Coloraction yellowaction = new Coloraction (color.yellow); Coloraction blueaction = new Coloraction (Color.Blue); Coloraction redaction = new Coloraction (color.red); Yellowbutton.addactionlistener (yellowaction); Bluebutton.addactionlistener (blueaction); Redbutton.addactionlistener (redaction);} Private class Coloraction implements Actionlistener{private color Backgroundcolor;public coloraction (color c) {backgroundcolor=c;} public void actionperformed (ActionEvent event) {buttonpanel.setbackground (backgroundcolor);}}}
Modify the background color of the panel