gpt4 book ai didi

java - 无法更改 JOptionPane 的背景颜色

转载 作者:行者123 更新时间:2023-12-02 08:33:23 25 4
gpt4 key购买 nike

您好,我创建了示例 progeam,它将提供与确认对话框相同的外观和感觉,并将背景颜色设置为红色。但我不知道我的选项背景颜色显示为默认颜色而不是红色是什么问题。我还需要跨所有平台的确认对话框具有相同的外观和感觉。

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

公共(public)类 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!");
}

}

最佳答案

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

here代码片段

这段代码在 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!");
}

}

关于java - 无法更改 JOptionPane 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com