I'm doing a tutorial and would add a background image. I've tried to do this:
public void run() {
JFrame frame = new JFrame("VisLibDemo");
try{
frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
}catch(IOException e){
System.out.println("Error");
}
frame.setMinimumSize(new Dimension(500, 400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new VisLibDemo());
frame.pack();
frame.setVisible(true);
}
However I am not getting.
Or not showing the background image, or when it appears is on top and the rest will not appear.
This is what I have:
And I wanted to add image to background...
I do this:
public void run() {
ImageIcon icon = new ImageIcon(
getClass().getResource("/Images/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
JFrame frame = new JFrame("VisLibDemo");
//frame.add(new VisLibDemo());
frame.setContentPane(label);
VisLibDemo demo = new VisLibDemo();
demo.setOpaque(false); frame.add(demo);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
But I can only see a little bit of the background image ("/Images/Brg800.jpg")
Sorry for the bad explanation. But I need some help!
Can help me Please!!
Set the layout to the
JLabel
Side Notes
When dealing with images that will be embedded into your program, you will want to load by resource and not by
File
which will not work on other systems beside the one you are developing on. To load by resource useYour image should be in your
src
somewhere, where it will be built into the class path. So with this file structureyou would use this
See more at the link at the bottom of embedded resources tag wiki
Here's an example of all the above fore-mentioned