具有不同背景的JOptionPane [英] JOptionPane with different background

查看:80
本文介绍了具有不同背景的JOptionPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过UIManager常量更改JOptionPane的背景颜色,文本区域和按钮区域的背景不受影响.

I'm trying to change background color of my JOptionPane through UIManager constants, the backgrounds of the text area and the button area are not affected.

我已经发布了我的代码:3

I have posted my code :3

图片

我也该如何修改他们的背景?

How can I modify their backgrounds too?

public LoginFINAL() {
    this.setImagen("/imagenes/FondoLogin.png");
    UIManager.put("OptionPane.background",Color.RED );  
    UIManager.put("Panel.background",Color.RED);  
    UIManager.put("Button.background",Color.RED); 
    initComponents();
    jLabel3.setVisible(false);
    setLocationRelativeTo(null);
    textUsers.requestFocus();


}

这就是我所说的JoptionPane

And this is how i call the JoptionPane

else{
          JOptionPane.showMessageDialog(rootPane, "pls");

    }

现在好点了吗?

推荐答案

我遇到了同样的问题,这是我的解决方案:

I had the same problem, here is my solution:

JOptionPane图片

对于图像中有问题的任何人,我都找到了/采用了解决方案.在我的系统上,无论我使用UIManager解决方案(如其他人发布的那样)还是制作JDialog并使用jd.getContentPane().setBackground(Color.white),我都得到了结果.因此,这是我想出的解决方法,其中您递归遍历JOptionPane中的每个组件,并设置每个JPanel的背景色:

For anyone having the problem in the 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);

先前已创建JOptionPane paneJDialog jd的位置.希望这对遇到此问题的人有所帮助.

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

这篇关于具有不同背景的JOptionPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆