0

嗨,我已经创建了示例程序,它将提供与确认对话框相同的外观和感觉,并将背景颜色设置为红色。但我不知道我的选项背景颜色显示为默认颜色而不是红色是什么问题。我也需要在所有平台上确认对话框的外观和感觉相同。

这是我编写的代码。请帮我解决问题

公共类 JOptionPaneBackground {

public static void main(String[] args) throws Exception {
    // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
    List<Object> keys = new ArrayList<Object>(uiDefaults.keySet());
    Collections.sort(keys, new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            return (o1.toString()).compareTo(o2.toString());
        }
    });
    for (Object key : keys) {
        System.out.println(String.format("%-40s = %s", key, uiDefaults.get(key)));
    }

    UIManager.put("OptionPane.background", Color.red);
    UIManager.put("Panel.background", Color.red);

    JOptionPane.showConfirmDialog(null, "Hello World!");
}

}

4

1 回答 1

2

我认为您需要获取 UIManager 的新实例并在其上设置窗格的颜色属性。

在此处查找代码片段

这段代码在 Windows 机器上对我来说很好:

public class JOptionPaneBackground {

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    UIManager.put("OptionPane.background", Color.RED);
    UIManager.put("Panel.background", Color.RED);

    JOptionPane.showConfirmDialog(null, "Hello World!");
}

}

于 2010-04-12T04:58:09.030 回答