swing (输入框)背景图片JTextPane

import javax.swing.JFrame;
import javax.swing.JTextPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Graphics;

public class Test {

    private JFrame frame;
    private JTextPane textP;
    private JScrollPane sPan;
    private ImageIcon imageIcon;

    public Test() {
        jbInit();
    }

    public void jbInit() {
        frame = new JFrame("给JTextPane加入底图");
        String url = "C:\\bg.jpg";
        imageIcon = new ImageIcon(url);
        textP = new JTextPane() {
            Image image = imageIcon.getImage();
            {
                setOpaque(false);
            }
            @Override
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, this);
                super.paintComponent(g);
            }
        };
        sPan = new JScrollPane(textP);
        frame.getContentPane().add(sPan);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setSize(230, 160);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

你可能感兴趣的:(java,C++,c,swing,C#)