JOptionPane 背景图像

发布于 2024-08-28 20:42:42 字数 56 浏览 3 评论 0原文

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

How to set background Image of JOptionPane? I want to show a different image on background of an JOptionPane.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-09-04 20:42:42

您可以扩展 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);
         }
    }

You could extend the JOptionPane class and override the paint method.

Edit:
Depending on the image resolution and quality you might be able to stretch it with an AffineTransform during the WindowResize events without to much distortion. This would let you handle the JOptionPane and image size discrepancies mentioned below.

   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);
         }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文