java入门 常见问题0007:如何让JTextField文本框可以设置背景图像

java编程入门 常见问题0007:如何让JTextField文本框可以设置背景图像

代码参考网友的代码,地址已经找不到了,对不住那位网友了

有的同学在做GUI开发时,希望文本框有背景图像,但是JTextField没有设置图像方法。那么怎么办呢?

一言不合就定义子类,定义JTextFiel使得它在构造的时候能指定图像(当然如果需要,也可以增加SetImage方法)

效果如下:

我们,来看看代码把:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

//定义文本框子类,使得能绘制背景图像
class TextFieldWithImage extends JTextField {
	BufferedImage img;
	TexturePaint texture;

	public TextFieldWithImage(File file) throws IOException {
		super();
		img = ImageIO.read(file);
		Rectangle rect = new Rectangle(0, 0, img.getWidth(null), img.getHeight(null));
		texture = new TexturePaint(img, rect);
		setOpaque(false);
	}

	@Override
	// 重写父类方法,参考JDK可知,当Swing组件的paint方法被调用时,paintComponent、paintBorder、
	// paintChildren这三个方法也会被按顺序调用,
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setPaint(texture);
		g.fillRect(0, 0, getWidth(), getHeight());
		super.paintComponent(g);
	}
}

//使用自动以按钮类
public class BackGroundImage extends JFrame {

	private JPanel contentPane;
	ImageIcon imageIcon = new ImageIcon("image\\001.png");
	JLabel background = new JLabel(imageIcon);
	JTextField foregroundtxt = new JTextField();
	TextFieldWithImage aaa;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BackGroundImage frame = new BackGroundImage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BackGroundImage() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		getLayeredPane().setLayout(null);
		try {
			aaa = new TextFieldWithImage(new File("image\\001.png"));
		} catch (Exception e) {
			// TODO: handle exception
		}

		aaa.setBounds(20, 20, 300, 100);
		contentPane.add(aaa); // 将文本框添加到前景
		aaa.setFont(new Font("宋体", Font.BOLD, 20));
		aaa.setForeground(Color.red);
	}
}

 

 

如果希望这种效果的,请参考https://blog.csdn.net/hiyohu/article/details/13512117,方法同上,只是让文本框设置的边缘宽度margin,将图片插入到marge中,文本从边缘宽度后开始

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曹红杏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值