如何将背景图像添加到JTextField? [英] How to add background image to JTextField?

查看:110
本文介绍了如何将背景图像添加到JTextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将背景图像添加到JPanel(创建扩展JPanel的ImagePanel类并重载它的paintComponent()方法),但是使用JTextField的这一技巧无法正常工作:显示图像,而不显示文本. 那么,如何正确地将背景图像添加到JTextField?

I know how to add background image to JPanel (creating ImagePanel class that extends JPanel and overload it's paintComponent() method), BUT this trick with JTextField not working properly: Displays image, but not text. So, how to add background image to JTextField properly?

推荐答案

您需要将文本字段添加到标签.像这样:

You need to add the text field to the label. Something like:

JTextField textField = new JTextField(10);
textField.setOpaque( false );
JLabel label = new JLabel( new ImageIcon(...) );
label.setLayout( new BorderLayout() );
label.add( textField );

这篇关于如何将背景图像添加到JTextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆