JMenuItem ImageIcon 太大 [英] JMenuItem ImageIcon too big

查看:27
本文介绍了JMenuItem ImageIcon 太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题.我的图像太大,所以它放大了相应的 JMenuItem.我不想像

I've encountered a problem. My image is too big so it enlarges the corresponding JMenuItem. I don't want to develop bicycles like

ImageIcon image = new ImageIcon(new ImageIcon("/home/template/img.jpg")
        .getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));

还有其他方法可以实现吗?

Is there any other way to accomplish that?

推荐答案

如果您不想在代码中缩放图像(我想这就是您在问题中的意思?),为什么不直接制作源图像大小开始?最简单的解决方案有时是最好的!

If you don't want to scale the image in code (I presume that's what you mean in the question?), why not just make the source image that size to start with? The simplest solution is sometimes the best!

此外,getScaledInstance() 通常是个坏主意. 解释了原因并提供了更好的调整图像大小的选项.如果您在别处使用该图像,那么值得阅读该文章并使用更好的技术(例如 graphics.drawImage())对其进行缩放.但如果您只是在菜单中使用它,那么调整源图像的大小可能是最好的做法.

Also, getScaledInstance() is generally a bad idea. This explains why and gives better options for resizing the image. If you're using the image elsewhere, then it's worth reading up on that article and scaling it using a better technique (such as graphics.drawImage()). But if you're just using it in the menu, then resizing the source image is probably the best thing to do.

如果您要调整大小:

BufferedImage image = ImageIO.read("img.jpg");
BufferedImage ret = new BufferedImage(32,32,BufferedImage.TYPE_RGB);
ret.getGraphics().drawImage(image,0,0,32,32,null);

类似的东西应该给你图像,然后你可以使用 ret 创建你的 ImageIcon.

Something like that should give you the image, you can then create your ImageIcon using ret.

这篇关于JMenuItem ImageIcon 太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆