您的位置:首页 > 其它

给JTextAear添加背景图片

2015-09-20 19:58 302 查看
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"));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: