/**
* @ 绘制背景图片
* @param x :从(x, y)开始绘制图片
* @param y :从(x, y)开始绘制图片
* @param imgURL :图片链接
* @param simOpacity :设置图片类似透明度(0.0f --- 1.0f)
* @return Jpanel
*/
/*
public static JPanel setImgBackground(final int x, final int y, final float simOpacity, final URL imgURL)
{
JPanel j = new JPanel()// 设置登陆窗体背景图片,即重写paintComponent方法(不是paint方法,避免遮挡)
{
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
ImageIcon iconField = new ImageIcon(imgURL);
Image imgField = iconField.getImage();
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, simOpacity);
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(ac);
g2d.drawImage(imgField, 0, 0, imgField.getWidth(null), imgField.getHeight(null), this);
g.drawImage(imgField, x, y, null);
}
};
return j;
}
*/
public static class setImgBackground extends JPanel//继承Jpanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private URL imgURL;
private float simOpacity;
private int x;
private int y;
public setImgBackground(int x, int y, float simOpacity, URL imgURL)//构造方法初始化变量
{
this.x = x;
this.y = y;
this.simOpacity = simOpacity;
this.imgURL = imgURL;
}
@Override
public void paintComponent(Graphics g)//重写父类方法
{
// TODO Auto-generated method stub
ImageIcon iconField = new ImageIcon(imgURL);
Image imgField = iconField.getImage();
AlphaComposite ac = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, simOpacity);
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(ac);
g2d.drawImage(imgField, 0, 0, imgField.getWidth(null), imgField.getHeight(null), this);
g.drawImage(imgField, x, y, null);
}
}
用上面的成员方法(即注释掉的部分),不能创建一个新的带背景的JPanel。需要用下面的类。
UI(JAVA)错误日志
最新推荐文章于 2024-09-23 16:59:28 发布