简体   繁体   中英

How to change background color at JFrame

I want to use JFrame to send system notification from my Java application, and I want set a background color at this notification. Now the notification works, but I'm not able to change the background color. This is the code:

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"));
    }
}

With this code the background color of my JFrame is everty time Gray.

You're actually wanting to change the JFrame's contentPane, not the JFrame itself.
Change

setBackground(Color.red);

to

getContentPane().setBackground(Color.red);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM