2010-04-13 222 views
4

如何設置背景圖片的JOptionPane?我想在JOptionPane的背景上顯示不同的圖像。JOptionPane背景圖片

+0

我正在尋找解決方案,你發現了什麼? – trinity420 2016-07-10 18:14:58

回答

1

您可以擴展JOptionPane類並重寫paint方法。

編輯: 根據圖像分辨率和質量的不同,您可以在WindowResize事件期間使用AffineTransform對其進行拉伸,而不會出現太多扭曲。這可以讓你處理下面提到的JOptionPane和圖像大小差異。

class ImageBackgroundPane extends JOptionPane 
    { 
     private BufferedImage img; 

     public ImageBackgroundPane (BufferedImage image) 
     { 
      this.img = image; 
     } 

     @Override 
     public void paint(Graphics g) 
     { 
      //Pick one of the two painting methods below. 

      //Option 1: 
      //Define the bounding region to paint based on image size. 
      //Be careful, if the image is smaller than the JOptionPane size you 
      //will see a solid white background where the image does not reach. 
      g.drawImage(img, 0, 0, img.getWidth(), img.getHeight()); 

      //Option 2: 
      //If the image can be guaranteed to be larger than the JOptionPane's size 
      Dimension curSize = this.getSize(); 
      g.drawImage(img, 0, 0, curSize.width, curSize.height, null); 


      //Make sure to paint all the other properties of Swing components. 
      super.paint(g); 
     } 
    } 
+3

無法正常工作...請給我舉一個例子嗎? – 2010-04-14 05:31:39