如何设置JPanel的透明背景

如何设置JPanel的透明背景

本文介绍了如何设置JPanel的透明背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPanel 的背景设置为透明?



我的框架是有两个 JPanel s:




  • 图片面板

  • 功能面板
    $ b
  • 功能面板与图片面板重叠。
    图像面板用作背景,它从远程URL加载图像。


    功能面板上我想绘制形状。由于功能面板背景色,现在图像面板无法显示。



    我需要使功能面板背景透明,同时仍然绘制其形状,并且我希望 Image Panel 可见(因为它正在做图像的平铺和缓存功能)。



    我使用两个 JPanel 的,因为我需要分离图片和形状图。



    有没有一种方法使重叠的Jpanel具有透明背景?

    解决方案

    或者,考虑,在文章。您可以在玻璃窗格的 paintComponent()方法中绘制您的特征内容。



    附录:使用,我加入了一个图像:

      / *设置内容窗格,其中主GUI位于此处。 * / 
    frame.add(changeButton,BorderLayout.SOUTH);
    frame.add(new JLabel(new ImageIcon(img.jpg)),BorderLayout.CENTER);

    并修改了玻璃窗格的 paintComponent()方法:
    $ b

      protected void paintComponent(Graphics g){
    if(point!= null){
    Graphics2D g2d =(Graphics2D)g;
    g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setComposite(AlphaComposite.getInstance(
    AlphaComposite.SRC_OVER,0.3f));
    g2d.setColor(Color.yellow);
    g2d.fillOval(point.x,point.y,120,60);
    }
    }


    Can JPanels background be set to transparent?

    My frame is has two JPanels:

    • Image Panel and
    • Feature Panel.

    Feature Panel is overlapping Image Panel.The Image Panel is working as a background and it loads image from a remote URL.

    On Feature Panel I want to draw shapes. Now Image Panel cannot be seen due to Feature Panel's background color.

    I need to make Feature Panel background transparent while still drawing its shapes and I want Image Panel to be visible (since it is doing tiling and cache function of images).

    I'm using two JPanel's, because I need to seperate the image and shape drawing .

    Is there a way the overlapping Jpanel have a transparent background?

    解决方案

    Alternatively, consider The Glass Pane, discussed in the article How to Use Root Panes. You could draw your "Feature" content in the glass pane's paintComponent() method.

    Addendum: Working with the GlassPaneDemo, I added an image:

    /* Set up the content pane, where the "main GUI" lives. */
    frame.add(changeButton, BorderLayout.SOUTH);
    frame.add(new JLabel(new ImageIcon("img.jpg")), BorderLayout.CENTER);
    

    and altered the glass pane's paintComponent() method:

    protected void paintComponent(Graphics g) {
        if (point != null) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.3f));
            g2d.setColor(Color.yellow);
            g2d.fillOval(point.x, point.y, 120, 60);
        }
    }
    

    image http://i41.tinypic.com/2j0mlv9.png

    这篇关于如何设置JPanel的透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:10