Unable to change background colour for JOptionPane
Hi I have created sample progeam which will give same look and feel of a confirm dialog and set the background colour as red. But I donot know what is the problem that my background colour of option is displayed as the default colour but not red. also i need same look and feel of confirm dialog across all platforms.
Here is the code that I have written. please help me to solve the issue
public class 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 ArrayLis开发者_如何学Pythont<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!");
}
}
I think you need to get a new instance of UIManager and set the color property of the pane on that.
Look here for the code snippet
This code works fine for me on a windows machine:
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!");
}
}
精彩评论