给JTextAear添加背景图片

来源:互联网 发布:c语言键盘钩子 编辑:程序博客网 时间:2024/05/18 02:31
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;


//重写JTextArea的绘画方法paint(Graphics g)
public class JTextAreaWithIcon extends JTextArea
{
private static final long serialVersionUID = 1L;
private Image image;

/**
* 带有参数的构造函数
* @param ximageIcon 要添加的背景图片
*/
public JTextAreaWithIcon( ImageIcon ximageIcon)
{
ImageIcon imageIcon = ximageIcon;
this.image = imageIcon.getImage();
setOpaque(false);
}
/**
* 无参构造,即不设置背景图
*/
public JTextAreaWithIcon( )
{
ImageIcon imageIcon = new ImageIcon();
this.image = imageIcon.getImage();
setOpaque(false);
}
/**
* Overriding 重写方法
*/
public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this);
super.paint(g);
}

}


/***用这个类来代替JTextArea实例化就ok了***/

jTextArea1 = new JTextAreaWithIcon(new ImageIcon("C:\\Users\\Lavee\\Desktop\\icon\\welcomeall.jpg"));



0 0
原创粉丝点击