设置文本背景颜色?

7
如何在 JOptionPane 中设置文本背景颜色?
图片: enter image description here
UIManager UI = new UIManager();
UI.put("OptionPane.background", Color.white);
UIManager.put("Button.background", Color.white);
UI.put("Panel.background", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.messagebackground", Color.white);
UI.put("OptionPane.textbackground", Color.white);
UI.put("OptionPane.warningDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.warningDialog.border.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.border.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.messageForeground", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.errorDialog.border.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.errorDialog.titlePane.shadow", Color.white);

JOptionPane.showMessageDialog(null, "Hello world", "HELLO WORLD", JOptionPane.INFORMATION_MESSAGE);

2
您的问题已解决吗?如果是,请将您所做的操作作为答案发布。 - 137
3个回答

9
为什么不像下面所示,简单地在此处添加一个自定义的JPanel
JOptionPane.showMessageDialog(frame, getLabelPanel(), "Hello World!",
                                        JOptionPane.INFORMATION_MESSAGE);

您可以从某个方法中获取JPanel,例如:
private JPanel getLabelPanel() {
    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.BLUE);
    JLabel helloLabel = new JLabel("Hello World!", JLabel.CENTER);
    helloLabel.setForeground(Color.WHITE);
    panel.add(helloLabel);

    return panel;
}

输出:

PANEIMAGE


更新 1:

否则,您可以尝试使用以下内容来更改一切,

uimanager.put("OptionPane.background", Color.BLUE);
uimanager.put("OptionPane.messagebackground", Color.BLUE);
uimanager.put("Panel.background", Color.BLUE);

更新的输出:

OPTIONPANEIMAGE


如果这不是你想要的,为什么不使用JDialog呢?这样你就不需要与UIManager打交道,而且可以轻松解决问题。 - nIcE cOw
1
我认为这个问题明确要求一种改变 JOptionPane 背景颜色的方法,而不是绕过它的方法。 - 137
1
@DustinE.:这只是我提出的一种替代方案。有时候一个人所想的可能不是正确的方向。因此,从他人那里获得的想法可以通过其他方式实现相同的目标 :-) 虽然采纳了你的建议,并进行了更新 :-) - nIcE cOw
@mKorbel:啊哈,我明白了,你指的是原帖作者的“LookAndFeel”。 - nIcE cOw

2

尝试

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

谢谢您的建议,但是并没有起到作用,情况仍然一样。 - Faken143

2
最干净的方法是使用JDialog创建自己的JOptionPane替代品,就像nIcE cOw建议的那样。JOptionPane在其框架中包含了很多垃圾,并且其中几个组件根本不检查任何JOptionPane特定的属性。
如果您仍然坚持使用JOptionPane,并且只想为其中的某些部分更改背景色,而不是应用程序中的所有内容,请尝试设置整个对话框及其所有子组件的背景色,最好的方法是迭代所有窗口内容并在每个内容上调用setBackground()。这可以在UI类中完成,或者针对使用JOptionPane.createDialog()获得的对话框单独完成。大致如下:
void colorComponentTree(Component component, Color color) {
    if (component instanceof Container) {
        if (component instanceof JComponent) {
             ((JComponent) component).setBackground(color);
        }

        for (Component child : ((Container) component).getComponents()) {
             colorComponentTree(child, color);
        }
    }
}

但是那真的很丑,我强烈建议采用自定义对话框的方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接