0

I am having some problems with setting an image as a background. The image will only show when I move the entire window. Below is a fragment of my code including setting a background image.

playButton = new JButton(pbI);
instructionButton = new JButton(ibI);
quitButton = new JButton(qbI);

mainFrame = new JFrame("My Major Project");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 600);
mainFrame.setLocationRelativeTo(null);
mainFrame.setLayout(null);

mainFrame.setLayout(new BorderLayout());
mainFrame.setContentPane(new JLabel(new ImageIcon("background.gif")));
mainFrame.setLayout(new FlowLayout());

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
boy
  • 5
  • 3
  • please ignore the first couple of lines of code as they are image buttons – boy Jun 01 '15 at 01:08
  • Avoid using `ImageIcon`, it can have issues, consider using `ImageIO` instead. Using `JLabel` for this purpose is not the best idea in the world, as `JLabel` will only calculate it's preferred size from the icon and text properties, this means, it may crop it's content. You might consider using something like [this](http://stackoverflow.com/questions/13791984/add-an-background-image-to-a-panel/13792503#13792503) instead, which gives you control over how the `preferredSize` of the panel is calculated – MadProgrammer Jun 01 '15 at 01:13
  • ok thanks ill try that – boy Jun 01 '15 at 03:05

1 Answers1

0

In order to update the JFrame, you need to call mainFrame.repaint();.

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50