【问题标题】:Swing JOptionPane background color is differentSwing JOptionPane 背景颜色不同
【发布时间】:2018-01-22 07:37:23
【问题描述】:

我一直在尝试使用 JOptionPane 在 Swing 中显示一个消息框:

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

消息框如图here:

当我添加这一行时

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

选项窗格如图所示here:

我应该怎么做才能在消息框中获得纯白色背景?

【问题讨论】:

    标签: java swing joptionpane uimanager


    【解决方案1】:

    您需要将面板背景设为白色。见this answer

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

    更新

    我有以下代码,它对我来说工作正常。

    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 是 UIManager 的键列表

    【讨论】:

    • 没有帮助我。请有其他建议
    • @LakshmiNarasimhanRavichandran 复制我更新的代码并进行测试。
    • @Muhammd 那也无济于事。对不起!
    • @LakshmiNarasimhanRavichandran 我已经更新了我的代码,请尝试一下。
    • public void ShowMessage(final String message) { Thread t = new Thread(new Runnable(){ public void run(){ UIManager UI=new UIManager(); UIManager.put("OptionPane.background ", Color.WHITE); UIManager.put("Panel.background", Color.WHITE); UIManager.put("OptionPane.messagebackground", Color.WHITE); JOptionPane.showMessageDialog(frmStrykerVer, message, Error_Title, Error_Message); } }); t.start(); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2014-06-13
    • 2011-03-12
    • 2012-02-17
    • 2018-12-28
    相关资源
    最近更新 更多