How to change background color of JOptionPane?

前端 未结 5 716
走了就别回头了
走了就别回头了 2020-12-09 12:01

I have added JOptionPane to my application but I do not know how to change background color to white?

`int option = JOptionPane.showConfirmDialog(bcfiDownloa         


        
5条回答
  •  半阙折子戏
    2020-12-09 12:12

    JOptionPane image

    For anyone having the same problem like above image, I found/adapted a solution. On my system, I got that result, whether I used the UIManager solution as others have posted, or made a JDialog and used jd.getContentPane().setBackground(Color.white). So here is the work-around I came up with, where you loop recursively through each component in the JOptionPane, and set each JPanel's background color:

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

    In your code where you want to have the message pop-up, something along the lines of:

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

    Where JOptionPane pane and JDialog jd have previously been created. Hope this helps anyone who had that issue.

提交回复
热议问题