JTextArea背景不随滚动而移动位置

来源:互联网 发布:怎么删除淘宝上的好评 编辑:程序博客网 时间:2024/05/21 20:27
package Rong;
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class TextArea extends JFrame {
 private static final long serialVersionUID =4785452373598819719L;
 private JScrollPane sp = null;
 private JTextArea text = null;
 private ImageIcon imageIcon = null;
 public TextArea() {
  super("JTextArea");
  imageIcon = new ImageIcon("11.png");
  // 构造文本组件并使之透明
  text = new JTextArea();
  text.setOpaque(false);
  // 构造滚动组件并使之透明
  sp = new JScrollPane(text);
  sp.setOpaque(false);
  sp.getViewport().setOpaque(false);
  // 构造一个背景JPanel
  JPanel backdrop = new JPanel() {
   private static final longserialVersionUID = 1957203784117943458L;
   {
   this.setOpaque(false);
   this.setLayout(new BorderLayout());
   }
   public voidpaintComponent(Graphics g) {
   g.drawImage(imageIcon.getImage(), 0, 0, this);
   super.paintComponents(g);
   }
  };
  // 将滚动组件加入
  backdrop.add(sp);
  // 将背景组件加入窗体
  this.getContentPane().add(backdrop);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(360, 260);
  this.setVisible(true);
 }
 public static void main(String args[]) {
  new TextArea();
 }
}JTextArea背景不随滚动而移动位置