JLabel inside JLayeredPane

Started by
0 comments, last by LorenzoGatti 11 years ago

I am trying to create a JLayeredPane and would like to put a JLabel containing a JButton inside on top of a background image. As far as I understand I am supposed to add the button to the panel and then add the panel to the layeredpane. Then I add an image to the layeredpane as well. Finally I add the layeredPane to the JFrame. The button is drawing fine but when I add the image panel it does not display. I apologize for posting my all my code, I know no one likes to read through some one else's code but I commented it as best as I could to make it a bit easier. I really have not been able to find a solution to this problem anywhere. Any help appreciated :)

My two classes:

GUIFrame - http://pastebin.com/7MAByuzj

BackgroundPanel - http://pastebin.com/3KUU82hg

Advertisement
 @Override
        public void paintComponent(Graphics g){
                super.paintComponents(g);
                this.setVisible(true);
               
                g.drawImage(backgroundImage, 0, 0, window.getWidth(), window.getHeight(), null);
        }
The drawImage() call paints the background above panel children (super.paintComponents(g)), which seems blatantly wrong, but the real problem is probably add()ing two or three panels to the JFrame without specifying layout constraints, so that they visually replace each other at BorderLayout.CENTER.

N.B. Your JFrame has a built-in JLayeredPane, you probably don't need to build another.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement