`
sunxiang0918
  • 浏览: 45867 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

给JTextPane加入背景图片

    博客分类:
  • J2SE
阅读更多
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();  
      }  
  }  
   
  
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics