设置文字背景颜色?

时间:2013-07-29 09:18:48

标签: java swing jtextfield look-and-feel nimbus

如何在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);

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

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

答案 2 :(得分: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);
        }
    }
}

然而,真的丑陋,我认真推荐采用自定义对话路线。

相关问题