swing文本框添加背景图片

 效果图:

添加图片类

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.JTextField;

public class BackgroundJTextField extends JTextField {
    
    private static final long serialVersionUID = 5810044732894008630L;
    private TexturePaint paint;
    
    public BackgroundJTextField(File file) {
        super();
        try {
            BufferedImage image = ImageIO.read(file);
            Rectangle rectangle = new Rectangle(0, 0, image.getWidth(), image.getHeight());
            paint = new TexturePaint(image, rectangle);
            setOpaque(false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(paint);
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }
}

测试类

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class BackgroundJTextFieldTest extends JFrame {
    
    /**
     * 
     */
    private static final long serialVersionUID = -7475843275177290984L;
    private JPanel contentPane;
    private JTextField textField2;
    private JPanel panel1;
    private JTextField textField1;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BackgroundJTextFieldTest frame = new BackgroundJTextFieldTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public BackgroundJTextFieldTest() {
        setTitle("\u5E26\u80CC\u666F\u56FE\u7247\u7684\u6587\u672C\u57DF");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 250, 150);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(2, 1, 5, 5));
        
        panel1 = new JPanel();
        contentPane.add(panel1);
        
        textField1 = new JTextField();
        textField1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel1.add(textField1);
        textField1.setColumns(10);
        
        JPanel panel2 = new JPanel();
        contentPane.add(panel2);
        
        textField2 = new BackgroundJTextField(new File("src/image/b.jpg"));
        textField2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        panel2.add(textField2);
        textField2.setColumns(10);
    }
    
}

猜你喜欢

转载自blog.csdn.net/weixin_52473454/article/details/121703286
今日推荐