Sign in to follow this  

JLabel inside JLayeredPane

Recommended Posts

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 -

BackgroundPanel -

Share this post

Link to post
Share on other sites
        public void paintComponent(Graphics g){
                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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this