我下载并安装了最新的Java 9早期访问版本,很高兴地发现Windows上的Java Swing应用程序现在以正确的大小呈现组件。我的客户不再需要将我的应用程序以一半的预期宽度和高度使用于HiDPI显示器。
我认为一定有这样一个类,但我找不到它。
但是我注意到,在我的Swing应用程序中,我的自定义图标只被缩放为双倍的宽度和高度,使它们看起来很模糊。
我有一个完整的所有图标集,包括正常尺寸(例如foobar.png)和双倍宽度/高度(例如foobar@2x.png)。我使用苹果的命名约定。
如何让Java 9轻松找到并使用更高分辨率的图像版本,而无需手动编码?
代码示例可以使问题更清晰,那么Java 9是否有多分辨率图标类可供使用,使得以下代码能够编译和运行?
import javax.swing.*;
public class ScratchSpace {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("I work in HiDPI!");
// can I replace the following line with some magical new method that automatically loads the same icon at multiple resolutions
// so that Swing chooses the correct one when rendering, depending on the current scale of the current display?
Icon icon = new IconWithMultipleSizes("foobar.png");
JLabel label = new JLabel("I'm a label with an icon.", icon, SwingConstants.LEFT);
frame.setContentPane(label);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
我认为一定有这样一个类,但我找不到它。