JOptionPane图标在Windows 10中被裁剪

dan*_*z22 7 java swing windows-look-and-feel windows-10

我使用以下代码在Java Swing中显示错误对话框:

JOptionPane.showMessageDialog(null, "Arquivo de imagem não encontrado. Por gentileza, altere o caminho do arquivo.", "Erro",  JOptionPane.ERROR_MESSAGE);
Run Code Online (Sandbox Code Playgroud)

使用Windows 10默认外观通过:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Run Code Online (Sandbox Code Playgroud)

但图标看起来像是这样:

带有裁剪图标的对话框

关于如何解决这个问题的任何想法?

这是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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 5

看起来 Oracle 终于注意到了这一点,并将在 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();
}
Run Code Online (Sandbox Code Playgroud)

为什么这有效

在 Windows 上,提供给应用程序的图标已按当前 DPI 设置缩放(96 = 不缩放,120 = +25%,144 = +50%)。不幸的是,Java 总是假设图标的大小为 16x16 或 32x32,但事实并非如此。上面的代码利用sun.awt.shell.Win32ShellFolder2.getIconBits()Java使用的本机方法来获取 OS 图标,但提供了适当的大小。这种方法将来可能无法使用,因为Win32ShellFolder2它不是 API 的一部分,可能会被完全修改或删除,但到目前为止,这是保留本机图标的唯一方法。