You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JLabel加载PNG图片无法缩放及高DPI下像素化问题求助

高DPI屏幕下Swing JLabel图片像素化且无法缩放

我已阅读Stack Overflow上《Java / Swing how to deal with different screen DPI and density settings?》帖子,该帖针对JDK9+及高DPI屏幕下JLabel图片像素化问题,但未能解决我的问题。

旧项目使用PNG图片在低DPI屏幕显示正常(无像素化),但现在所有图片均出现像素化问题。为解决该问题我新建了Java项目并使用高分辨率PNG图片,然而无论如何尝试,图片都无法缩放到JLabel尺寸。

我的代码如下:

initComponents(); // 初始化JFrame组件... 
ImageIcon iconWorkHours = new ImageIcon(getClass().getResource("/horkhours/WorkHours.png"));
this.lblLogo.setPreferredSize(new Dimension(135, 133));
Image imageWorkHours = iconWorkHours.getImage();
imageWorkHours.getScaledInstance(lblLogo.getWidth(), lblLogo.getHeight(), Image.SCALE_SMOOTH);
BufferedImage bufImgWH = new BufferedImage(lblLogo.getWidth(), lblLogo.getHeight(), BufferedImage.TYPE_INT_ARGB);
bufImgWH.getGraphics().drawImage(imageWorkHours, 0, 0, null);
this.lblLogo.setIcon(new ImageIcon(bufImgWH));

结果始终是图片无法缩放,显示效果如下:
图片未适配JLabel尺寸


内容的提问来源于stack exchange,提问作者Methark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:21:02