Re: Background image on GUI
There are a few ways you can do this.
One way is to use an OverlayLayout: 1) create a JPanel and set its layout to OverlayLayout 2) add the ImageIcon to another newly created JPanel and add this to the panel created in step 1 3) create another JPanel and add your buttons there, add this JPanel to the JPanel created in step 1 4) add the JPanel created in step 1 to the JFrame.
Another way is to override the paintComponent method of the JPanel, load the image as an Image object, and then draw the image at the position you wish.
Both ways may require a bit of fiddling to position your buttons in the correct position.
For future reference, using the code tags to place your code into a post make your code a lot more readable for those of us trying to diagnose the problem.
Re: Background image on GUI
Thank you! I hope that edit makes it look better although I should have indented it more!!!
I will try tomorrow and see what happens!
Re: Background image on GUI
Code :
import javax.swing.*;
import java.awt.*;
class Intro implements Runnable
{
public static void main(String[] args)
{
Intro program = new Intro();
SwingUtilities.invokeLater(program);
}
public void run()
{
JFrame inicio = new JFrame("Videogame");
inicio.setDefaultCloseOperation(inicio.EXIT_ON_CLOSE);
inicio.setSize(800,600);
inicio.setLocationByPlatform(true);
inicio.setVisible(true);
JPanel bt = new JPanel();
bt.add(new JButton("START"));
bt.add(new JButton("QUIT"));
JPanel over = new JPanel();
LayoutManager overlay = new OverlayLayout(over);
over.setLayout(overlay);
JPanel imagen = new JPanel();
ImageIcon image = new ImageIcon("image.jpg");
imagen.add(new JLabel(image));
over.add(imagen);
over.add(bt);
inicio.add(over);
}
}
I hope my code is clearer now!!
It works fine except the second (QUIT) button takes a while to load and I don't understand why... any ideas? Sorry for the bother!