有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何更改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) 个答案

  1. # 1 楼答案

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

    UIManager.put("OptionPane.background", Color.WHITE);
    UIManager.getLookAndFeelDefaults().put("Panel.background", Color.WHITE);
    
  2. # 2 楼答案

    通过使用^{}

     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);
    
  3. # 3 楼答案

    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之前已创建。希望这能帮助任何有这个问题的人

  4. # 4 楼答案

    在nimbus look and feel中,这些代码都不可用

    所以解决办法是

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

    这也被称为“黑暗光环”添加到主框架顶部的主要方法。 因此,它将自动更改所有JOptionPane的背景

    而且你不能改变按钮的背景

    UIManager.put("OptionPane.buttonBackground", BLACK);
    

    所以你应该用

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

    记住——但不幸的是,这段代码会改变你所有的按钮ETC的背景。因此,您必须将*.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)。这是一种专业的软件,你可以根据自己的需要进行更改

  5. # 5 楼答案

    我是经理。放置(“OptionPane.background”,颜色为白色); 我是经理。getLookAndFeelDefaults()。放置(“面板.背景”,颜色.白色)

    在调用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);
     }
    
  6. # 6 楼答案

    使用类似的方法来改变背景颜色,只为这一条信息显示,而不是整个系统

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