JMenuItem ImageIcon太大了(JMenuItem ImageIcon too big)

编程入门 行业动态 更新时间:2024-10-17 11:33:11
JMenuItem ImageIcon太大了(JMenuItem ImageIcon too big)

我遇到了一个问题。 我的图像太大,所以它放大了相应的JMenuItem。 我不想开发自行车

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

有没有其他方法可以实现这一点?

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?

最满意答案

如果你不想在代码中缩放图像(我认为这就是你在这个问题上的意思?)为什么不把这个尺寸的源图像制作成开头? 最简单的解决方案有时是最好的!

另外,getScaledInstance()通常是一个坏主意。 这解释了为什么,并提供更好的选项来调整图像大小。 如果你在其他地方使用图像,那么值得阅读该文章并使用更好的技术(如graphics.drawImage())对其进行缩放,但如果您只是在菜单中使用它,则可能调整源图像的大小最好的事情要做。

如果你打算调整大小:

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。

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!

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.

If you are going to resize:

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

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

更多推荐

本文发布于:2023-07-16 12:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:太大了   JMenuItem   ImageIcon   big

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!