简体   繁体   English

我可以调整jlabel中图标的大小吗?

[英]Can I make resizable the icon inside a jlabel?

Is there any property or any code that I can implement to make an icon inside a JLabel resizable. 我可以实现可调整大小的JLabel的图标的任何属性或代码吗? Or is there any other item which can store an image, that I could use to have a resizable image inside a JFrame ? 还是还有其他可以存储图像的项目,可以在JFrame使用可调整大小的图像?

use this code : 使用此代码:

 photo = new ImageIcon(UrlofPhoto); 
 Image im = photo.getImage();
 Image Newim = im.getScaledInstance(yourlabel.getWidth(),  yourlabel.getHeight(),Image.SCALE_SMOOTH);
 ImageIcon Newphoto=new ImageIcon(Newim);
 yourlabel.setIcon(Newphoto);

don't forget to set an initialised size to yourlabel 不要忘记为标签设置初始化大小

Generally, I would't suggest trying this with a JLabel as JLabel has a lot of other features you really don't want to messing with (alignment and text). 通常,我不建议尝试使用JLabel此操作,因为JLabel还有很多您真的不想弄乱的其他功能(对齐和文本)。

Generally, a better solution is to use a dedicated "image" panel, which you can provide additional control over to fine tune how you want the scaling to work. 通常,更好的解决方案是使用专用的“图像”面板,您可以通过该面板提供其他控制来微调缩放的工作方式。

For example 举个例子

If you're really stuck on using JLabel , I would recommend attaching a ComponentListener to it and resizing the underlying image when ever it changes size. 如果您真的想使用JLabel ,我建议将ComponentListener附加到它,并在更改大小时调整基础图像的大小。 The problem with this is componentResized may be called repeatedly in quick succession, meaning you will need to devise some kind of coalescing algorithm that only reacts to the last event within a given period of time... 问题在于componentResized可能会快速连续地被重复调用,这意味着您将需要设计某种合并算法,该算法仅在给定的时间段内对最后一个事件做出反应...

查看Darryl的“ 拉伸图标” ,它将动态缩放图标以填充标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM