简体   繁体   English

当窗口调整大小时,如何在JLabel中调整图像大小? 如何保持适当的规模?

[英]How to resize an image in a JLabel as the window resizes? How to maintain proper scale?

Say I've got an image in a JLabel. 假设我在JLabel中有一张图片。 Here's how I'm currently adding it: 这是我当前添加的方式:

p=resizeImage(p,lbl.getWidth(),lbl.getHeight());
lbl.setIcon(p);

I'd like to resize this as the label is resized. 我想调整标签的大小。

  • How can I accomplish this? 我该怎么做?
  • how can I resize while maintaining proportion? 如何在保持比例的同时调整大小?

I could write a small function to get the image width to height ratio, get the label width and height, find the smaller of the two(proportionally) and calculate the new height and width manually. 我可以编写一个小函数来获取图像的宽高比,获取标签的宽度和高度,找到两者中较小的一个(按比例)并手动计算新的高度和宽度。 That would not be an issue, but I wonder if there's a cleaner way. 那不是问题,但是我想知道是否有更清洁的方法。

您可以收听标签的大小调整事件,或者,我更喜欢直接绘制图像(用JComponent替换JLabel)。

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

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