0

I would like to make an image as a background to my JFrame without removing the current components that I have added to the screen.

I am able to get and image on the screen, but as soon as I do the buttons that I have added disappear from the screen.

My current code puts the image onto the screen but removes the buttons. The line that does this is marked with the comment.

Any help/advice will be appreciated! -Thanks in advance

package game;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MainMenu extends JFrame{

    public MainMenu() {
        super("TEST");
    }

    public static void createGUI()
    {
        MainMenu frame = new MainMenu();
        frame.setSize(300,400);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /**
         *  This is the line that is giving me issues
        **/
        frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Austin\\Pictures\\Landscape.jpg")));

        panel(frame);

        frame.setVisible(true);
    }

    public static void panel(MainMenu frame)
    {
        JPanel panel = new JPanel(new GridBagLayout());

        frame.add(panel);
        GridBagConstraints c = new GridBagConstraints();


        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(4,4,4,4);
        panel.add(Buttons.singlePlayerButton(), c);

        c.gridx = 0;
        c.gridy = 1;
        panel.add(Buttons.twoPlayerButton(), c);

        c.gridx = 0;
        c.gridy = 2;
        panel.add(Buttons.exitButton(), c);


    }

    public static void runGUI()
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        createGUI();
                    }
                });
    }

}

2 Answers2

1

JFrame.add() is a shortcut for JFrame.getContentPane().add(). This means that by calling frame.add(panel); you're setting your JFrame's content pane once, and by later calling frame.setContentPane(new JLabel(...)); you're throwing away the old content pane and replacing it with a new JLabel you just made.

A better way to do what you want is to create a new class which is a subclass of JPanel, then override its paintComponent() method so it will paint your image directly on the background of the panel. Add all of your buttons etc. to a new instance of your custom JPanel subclass.

This is already described a few other places on Stack Overflow:

Background image JFrame with content

https://stackoverflow.com/a/22161361/635678

Community
  • 1
  • 1
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • I know I am not supposed to comment this, but I have been trying to figure this out for countless hours, but thanks to you I was able to solve it right away! Thanks again for your help!! –  Nov 12 '15 at 01:03
-1

Try to change "frame.add(panel);" to the position after you add the exit Button in your panel method. iI dont know if this work but you add the panel before you adding the Buttons. Seems little strange to me but maybe its correct.

Greets Cronical

*edit i think it should also be possible to add buttons after you add the panel to the frame but im not sure

Cronical
  • 34
  • 8