JMenuItem ImageIcon太大了 [英] JMenuItem ImageIcon too big

查看:120
本文介绍了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 。

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

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

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