给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.net.URL;  
  import   java.awt.Image;  
  import   java.awt.Graphics;  
  import   java.net.MalformedURLException;  
   
  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="http://www.csdn.net/images/Homeother/BTS2004.JPG";  
          try   {  
              imageIcon   =   new   ImageIcon(new   URL(url));  
          }  
          catch   (MalformedURLException   ex)   {  
          }  
          textP   =   new   JTextPane(){  
              Image   image   =   imageIcon.getImage();  
              {setOpaque(false);}  
              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()   {  
              public   void   windowClosing(WindowEvent   e)   {  
                  System.exit(0);  
              }  
          });  
          frame.setSize(230,   160);  
          frame.setVisible(true);  
      }  
   
      public   static   void   main(String[]   args)   {  
          new   Test();  
      }  
  }  
   
  
 

你可能感兴趣的:(java,.net,swing)