java怎么写一个能改变颜色的窗口啊

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

类似这样的

img

你可以参考一下,希望采纳

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

public class MyFrame extends JFrame {
    JPanel root,panel;
    JButton b1,b2,b3,b4;

    MyFrame(String title) {
        super(title);

        //设置顶层容器
        root = new JPanel();
        setContentPane(root);
        root.setLayout(new BorderLayout());

        //添加面板
        panel = new JPanel();
        root.add(panel,BorderLayout.SOUTH);

        //添加按钮
        b1 = new JButton("Red");
        b2 = new JButton("Green");
        b3 = new JButton("Blue");
        b4 = new JButton("Gray");

        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);

        b1.addActionListener((e)->{
            root.setBackground(Color.RED);
            panel.setBackground(Color.RED);
        });

        b2.addActionListener((e)->{
            root.setBackground(Color.GREEN);
            panel.setBackground(Color.GREEN);
        });

        b3.addActionListener((e)->{
            root.setBackground(Color.BLUE);
            panel.setBackground(Color.BLUE);
        });

        b4.addActionListener((e)->{
            root.setBackground(Color.GRAY);
            panel.setBackground(Color.GRAY);
        });

        //设置窗口风格
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //设置窗口显示位置和大小
        setBounds(400, 300, 400, 300);

        //设置窗口可见
        setVisible(true);
    }

    public static void main(String[] args) {
        MyFrame frame = new MyFrame("Change Color");
    }
}

运行结果:

img

img