如何更改JOptionPane的背景颜色?

时间:2012-01-30 13:54:48

标签: java swing background-color joptionpane

我已将JOptionPane添加到我的应用程序中,但我不知道如何将背景颜色更改为白色?

`int option = JOptionPane.showConfirmDialog(bcfiDownloadPanel,
            new Object[]{"Host: " + source, panel},
            "Authorization Required",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE
    );

    if (option == JOptionPane.OK_OPTION) {                  }`

6 个答案:

答案 0 :(得分:15)

使用UIManager

 import javax.swing.UIManager;

 UIManager UI=new UIManager();
 UI.put("OptionPane.background",new ColorUIResource(255,0,0));
 UI.put("Panel.background",new ColorUIResource(255,0,0));

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

 JOptionPane.showMessageDialog(null,"Text","SetColor",JOptionPane.INFORMATION_MESSAGE);

答案 1 :(得分:4)

JOptionPane image

对于在图像中出现问题的任何人,我找到/改编了一个解决方案。在我的系统上,我得到了这个结果,我是否使用了其他人发布的UIManager解决方案,或者创建了一个JDialog并使用了jd.getContentPane()。setBackground(Color.white)。所以这是我提出的解决方法,在那里递归循环遍历JOptionPane中的每个组件,并设置每个JPanel的背景颜色:

private void getComponents(Container c){

    Component[] m = c.getComponents();

    for(int i = 0; i < m.length; i++){

        if(m[i].getClass().getName() == "javax.swing.JPanel")
            m[i].setBackground(Color.white);

        if(c.getClass().isInstance(m[i]));
            getComponents((Container)m[i]);
    }
}

在您希望弹出消息的代码中,有以下内容:

pane = new JOptionPane("Your message here", 
                JOptionPane.PLAIN_MESSAGE ,JOptionPane.DEFAULT_OPTION);
        getComponents(pane);
        pane.setBackground(Color.white);
        jd = pane.createDialog(this, "Message");
        jd.setVisible(true);

之前已创建JOptionPane paneJDialog jd的位置。希望这可以帮助那些遇到这个问题的人。

答案 2 :(得分:2)

使用类似这样的内容来改变这一个消息显示的背景颜色而不是整个系统......

    Object paneBG = UIManager.get("OptionPane.background");
    Object panelBG = UIManager.get("Panel.background");
    UIManager.put("OptionPane.background", new Color(...));
    UIManager.put("Panel.background", new Color(...));

    int ret = messageBox(msg, null, (short)type);

    UIManager.put("OptionPane.background", paneBG);
    UIManager.put("Panel.background", panelBG);

答案 3 :(得分:1)

如果您遇到与erik k atwood相同的问题,请使用此代码。这解决了这个问题:

UIManager.put("OptionPane.background", Color.WHITE);
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.WHITE);

答案 4 :(得分:1)

UIManager.put(“ OptionPane.background”,Color.WHITE); UIManager.getLookAndFeelDefaults()。put(“ Panel.background”,Color.WHITE);

在调用JOptionPane对话框之前输入以下代码:

UIManager.put("OptionPane.background", Color.decode("#3c6562"));
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.decode("#3c6562"));
    
int input= JOptionPane.showConfirmDialog
(
   null,
   "Close the programm?",
   "Exit",
   JOptionPane.YES_NO_OPTION
 );

 if(input == 0)
 {
    System.exit(0);
 }

答案 5 :(得分:0)

在 nimbus 外观和感觉中,所有代码都无法使用。

解决办法是,

UIManager.put("control", new Color(0, 0, 0));

这也称为“Dark Nimbus”,将此添加到主框架的主要方法的顶部。 所以它会自动改变所有JOptionPane的背景。

而且你也不能用

改变按钮背景
UIManager.put("OptionPane.buttonBackground", BLACK);

所以你应该使用,

UIManager.put("nimbusBase", new Color(0, 0, 0));

请记住 - 但不幸的是,此代码将更改您所有按钮等的背景。因此,您必须将 *.setBackground(...); 添加到所有其他对象。

如果你想改变 JOptionPane 的前景,你应该使用

UIManager.put("text", new Color(255, 255, 255));

再次不幸的是,这将改变您所有文本的前景。

这些代码都被称为暗光云。

如果您正在使用 nimbus,您可以尝试使用这些 UIManager 代码来自定义 nimbus 的外观和感觉。

UIManager.put("control", new Color(0, 0, 0));
UIManager.put("info", new Color(0, 0, 0));
UIManager.put("nimbusBase", new Color(0, 0, 0));
UIManager.put("nimbusAlertYellow", new Color(248, 187, 0));
UIManager.put("nimbusDisabledText", new Color(255, 255, 255));
UIManager.put("nimbusFocus", new Color(115, 164, 209));
UIManager.put("nimbusGreen", new Color(176, 179, 50));
UIManager.put("nimbusInfoBlue", new Color(66, 139, 221));
UIManager.put("nimbusLightBackground", new Color(0, 0, 0));
UIManager.put("nimbusOrange", new Color(191, 98, 4));
UIManager.put("nimbusRed", new Color(169, 46, 34));
UIManager.put("nimbusSelectedText", new Color(255, 255, 255));
UIManager.put("nimbusSelectionBackground", new Color(18, 134, 175));
UIManager.put("text", new Color(255, 255, 255));

你可以试试这些代码。在我的项目中,灵气看起来像

enter image description here

enter image description here

enter image description here

但我总是推荐使用“Flatleaf”(搜索谷歌“FlatLafLookAndFeel”或去jar.download.com“)。它是专业的,你可以自己改变。