【问题标题】:JOptionPane with different background不同背景的 JOptionPane
【发布时间】:2014-06-13 06:47:58
【问题描述】:

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

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

Image

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

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 的方式

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

    }

现在好点了吗?

【问题讨论】:

  • 消息和按钮的背景应该是蓝色的
  • 图片是你的代码吗?
  • 是的...之后我调用 JoptionPane 来显示此消息...如果您需要更多代码,请告诉我您想看什么
  • 相同的结果...嗯,我拍了一张将外观设置为主要类的部分的照片...有帮助吗? imgur.com/TQ1Cs7V
  • 我仍然不知道你是怎么得到这张照片的,因为我是在我的电脑上拍的,一切都变成了蓝色。你能分享你所有的代码吗?

标签: java swing joptionpane


【解决方案1】:

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

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 的位置。希望这对遇到这个问题的人有所帮助。

【讨论】:

  • 我们如何改变消息的背景颜色?它确实改变了窗格和面板。
猜你喜欢
  • 2018-01-22
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2015-08-14
相关资源
最近更新 更多