我使用以下代码在Java Swing中提供错误对话框:
JOptionPane.showMessageDialog(null, "Arquivo de imagem não encontrado. Por gentileza, altere o caminho do arquivo.", "Erro", JOptionPane.ERROR_MESSAGE);
使用Windows 10默认外观:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
但是图标看起来像是这样:
关于如何解决的任何想法?
这是SSCCE:
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class SSCCE {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JOptionPane.showMessageDialog(null, "Error message", "Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
看起来甲骨文终于注意到了这一点和will fix it in JDK 9,但暂时对于那些不愿意提供自定义图标的人来说,这是一个qiuck,hacky和Windows依赖的解决方案。
在致电UIManager.setLookAndFeel()
之后插入此代码:
try
{
String[][] icons =
{
{"OptionPane.errorIcon", "65581"},
{"OptionPane.warningIcon", "65577"},
{"OptionPane.questionIcon", "65579"},
{"OptionPane.informationIcon", "65583"}
};
//obtain a method for creating proper icons
Method getIconBits = Class.forName("sun.awt.shell.Win32ShellFolder2").getDeclaredMethod("getIconBits", new Class[]{long.class, int.class});
getIconBits.setAccessible(true);
//calculate scaling factor
double dpiScalingFactor = Toolkit.getDefaultToolkit().getScreenResolution() / 96.0;
int icon32Size = (dpiScalingFactor == 1)?(32):((dpiScalingFactor == 1.25)?(40):((dpiScalingFactor == 1.5)?(45):((int) (32 * dpiScalingFactor))));
Object[] arguments = {null, icon32Size};
for (String[] s:icons)
{
if (UIManager.get(s[0]) instanceof ImageIcon)
{
arguments[0] = Long.valueOf(s[1]);
//this method is static, so the first argument can be null
int[] iconBits = (int[]) getIconBits.invoke(null, arguments);
if (iconBits != null)
{
//create an image from the obtained array
BufferedImage img = new BufferedImage(icon32Size, icon32Size, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, icon32Size, icon32Size, iconBits, 0, icon32Size);
ImageIcon newIcon = new ImageIcon(img);
//override previous icon with the new one
UIManager.put(s[0], newIcon);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
在Windows上,提供给应用程序的图标已按当前DPI设置进行缩放(96 =无缩放,120 = + 25%,144 = + 50%)。
不幸的是,Java总是假设图标的大小为16x16或32x32,但实际情况并非如此。
上面的代码使用Java使用的本机方法sun.awt.shell.Win32ShellFolder2.getIconBits()
来获取操作系统图标,但提供了适当的大小。
这种方法将来可能无法使用,因为Win32ShellFolder2
不是API的一部分,可能会被完全修改或删除,但到目前为止,这是保留原生图标的唯一方法。
答案 1 :(得分:1)
mkJ720的答案很好用,但是图标ID已关闭。这是更正的:
String[][] icons = {
{"OptionPane.warningIcon", "65581"},
{"OptionPane.questionIcon", "65583"},
{"OptionPane.errorIcon", "65585"},
{"OptionPane.informationIcon", "65587"}
};