如何添加一個標籤面板內,並繪製使用setIcon(..)
我通常使用下面的類,以使圖像適合標籤尺寸圖像(我把我的標籤具有靜態大小 - 不調整大小) ..你可能要修改它以滿足您的需要..
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.URL;
import javax.swing.ImageIcon;
public class CustomImageIcon extends ImageIcon {
private BufferedImage dest;
public CustomImageIcon(String filename) {
super(filename);
}
public CustomImageIcon(Image image) {
super(image);
}
public CustomImageIcon(URL location) {
super(location);
}
@Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
if(c!=null)
dest = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
else dest = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB);
ImageObserver imgObs = getImageObserver();
if(imgObs==null) imgObs = c;
int width;
int height;
if(c!=null)
{
width = c.getWidth();
height = c.getHeight();
}
else
{
width = getIconWidth();
height = getIconHeight();
}
g.drawImage(dest, 0, 0, c);
g.drawImage(
getImage(),
0,
0,
width,
height,
imgObs);
}
}
很抱歉,如果它是模糊的,我現在已經列入其繪製圖像的代碼。 – user3352349
@ user3352349'我現在已經包含了繪製圖像的代碼 - - 沒有幫助。仍然沒有幫助。我們仍然不知道如何調用draw()方法。看起來您正在繪製BufferedImage,但我們不知道實際將BufferedImage繪製到面板的位置,或者您不使用JLabel顯示BufferedImage的原因。你添加了調試代碼來顯示尺寸變量的尺寸大小嗎?同樣,如果您需要更多幫助,請選擇適當的SSCCE – camickr