如何在JFrame上更改背景颜色

bir*_*tri 2 java swing jframe

我想使用JFrame从我的Java应用程序发送系统通知,我想在此通知中设置背景颜色.现在通知有效,但我无法更改背景颜色.这是代码:

public class NotificationFrame extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = -2902763674924791105L;

    public NotificationFrame(){
        super();
        setUndecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.red);
        setMinimumSize(new Dimension(300, 100));
        add(new LabelFormat("Notifiche"));
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,我的JFrame的背景颜色是格雷德时间.

Hov*_*els 7

您实际上想要更改JFrame的contentPane,而不是JFrame本身.
更改

setBackground(Color.red);
Run Code Online (Sandbox Code Playgroud)

getContentPane().setBackground(Color.red);
Run Code Online (Sandbox Code Playgroud)