2010-04-13 219 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