简体   繁体   中英

Swing JOptionPane background color is different

I have been trying to display a messagebox in Swing with JOptionPane by using:

JOptionPane.showMessageDialog(Component parentComponent,
                                     Object message,
                                     String title,
                                     int messageType)
                              throws HeadlessException

The message box looks as shown here :

When I add this line

UIManager.put("OptionPane.background", Color.white);

Option pane looks as shown here :

What should I do in order to get a pure white background in the message box?

You need to make the panel background also white. See this answer

     UIManager UI = new UIManager();
     UI.put("OptionPane.background", Color.white);
     UI.put("Panel.background", Color.white);

Update

I have the following code and it is working fine for me.

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

public class Dialog {
    public static void main(String[] args){
        UIManager.put("OptionPane.background", Color.WHITE);
        UIManager.put("OptionPane.messagebackground", Color.WHITE);
        UIManager.put("Panel.background", Color.WHITE);
        JOptionPane.showMessageDialog(null, "Invalid Com Port!", "SetColor", JOptionPane.ERROR_MESSAGE);
    }
}

Here is a list of the keys for the UIManager 在此处输入图片说明

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