【问题标题】:Setting text background color?设置文本背景颜色?
【发布时间】:2013-07-29 00:22:16
【问题描述】:

如何设置JOptionPane中的文字背景颜色?

图片:

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

【问题讨论】:

  • 您的问题解决了吗?如果是这样,请发布您所做的作为答案。

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


【解决方案1】:

你为什么不简单地在这个地方添加一个自定义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;
}

输出:


更新 1:

否则你可以试试这个来改变一切,

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

更新输出:

【讨论】:

  • @user2464161 : 如果这不是你想要的,为什么不使用JDialog,这样你就不需要玩UIManager 并制定一个没有任何大惊小怪的解决方案.
  • 我相信这个问题明确要求一种方法来改变 JOptionPane 的背景颜色而不是绕过它。
  • @DustinE。 : 这只是我建议的替代方案。有时一个人在想什么,可能不是正确的轨道。所以它是关于一个人从别人那里得到的想法,同样的事情也可以通过其他方式实现:-)虽然采纳了你的建议,并且同样更新了:-)
  • @mKorbel : 啊哈,我刚明白,你指的是 OP 那边LookAndFeel
【解决方案2】:

试试

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

【讨论】:

  • 感谢您的建议,但没有帮助,还是一样
【解决方案3】:

最干净的方法是使用 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);
        }
    }
}

然而,这真的很难看,我强烈建议采用自定义对话路线。

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2010-09-17
    相关资源
    最近更新 更多