How to leave more than one transparent panel?

4

Implementing the response response, I was able to leave a transparent panel. However, to be able to organize the way I want, I wanted to use more JPanels , with different layout managers.

But when I do this, they do not become transparent. How could I do that?

Problem example:

Theimageisjustthisblueshapethere,thisgray/whitebackgroundisthecomponent.

WhenIuseasinglepanel:

importjavax.swing.*;importjava.awt.*;importjava.awt.image.BufferedImage;importjava.io.IOException;importjavax.imageio.ImageIO;publicclassTeste{publicTeste(){JWindowjWindow=newJWindow();jWindow.setBackground(newColor(0,0,0,0));jWindow.setContentPane(newPane());jWindow.pack();jWindow.setVisible(true);jWindow.setLocationRelativeTo(null);}classPaneextendsJPanel{privateBufferedImageleaf;publicPane(){//setLayout(newBorderLayout());JPanelborderPainel=newJPanel();borderPainel.setLayout(newBorderLayout());JPanelgridPainel=newJPanel();gridPainel.setLayout(newGridLayout(2,1));gridPainel.add(newJLabel("Label 01"));
            gridPainel.add(new JLabel("Label 02"));

            borderPainel.add(gridPainel, BorderLayout.SOUTH);
            add(borderPainel);

            try {
                leaf = ImageIO.read(getClass().getResource("/imagens/icon.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            gridPainel.setOpaque(false);
            borderPainel.setOpaque(false);
            //setOpaque(false);

        }

        @Override
        public Dimension getPreferredSize() {
            return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (leaf != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(leaf, 0, 0, this);
                g2d.dispose();
            }
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        new Teste();
    }
}
    
asked by anonymous 01.04.2018 / 19:59

1 answer

4

I made 3 changes to the code:

  • I have already warned you several times in other posts, but notice again: ALWAYS dispatch graphical application of this API to

01.04.2018 / 20:21