4

如何设置 JOptionPane 的背景图片?我想在 JOptionPane 的背景上显示不同的图像。

4

1 回答 1

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);
         }
    }
于 2010-04-13T13:22:11.823 回答