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();
}
});
}
}