代码之家  ›  专栏  ›  技术社区  ›  AZ_

JOptionPane背景图像

  •  4
  • AZ_  · 技术社区  · 15 年前

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

    1 回复  |  直到 15 年前
        1
  •  1
  •   Tansir1    15 年前

    编辑:

       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);
             }
        }