How to set background image in Java for a JFrame
__________________________________________________________________________
In This Java Code We Will See How You Can Make A BackGround Image For Your JFrame In Java Using A JLabel Programming Language.
  
      
    
  
  
In This Java Code We Will See How You Can Make A BackGround Image For Your JFrame In Java Using A JLabel Programming Language.
Method 1: image from Desktop.
Source Code: 
package learn;
import java.awt.FlowLayout;
import javax.swing.*;
import java.io.IOException;
import java.io.*;
import javax.imageio.*;
public class Test2 extends JFrame{
    public Test2(){
    super("window");
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try{
        setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:\\Users\\samsng\\Documents\\NetBeansProjects\\Learn\\src\\learn\\images\\sparta.jpg")))));
    }catch(IOException e)
    {
        e.printStackTrace();
    }
    this.setLayout(new FlowLayout());
    this.setResizable(false);
    this.pack();
    this.setVisible(true);
    }
    public static void main(String[] args) {
        Test2 t = new Test2();
    }
}
Method 1: image from Resource.
Step 1: create a new package.
Step 2: add an image to the package.
Source Code:
package learn;
import javax.swing.*;
public class Test extends JFrame{
public Test(){
super("window");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pan = new JPanel();
JLabel jlb = new JLabel();
jlb.setIcon(new ImageIcon(getClass().getResource("images//sparta.jpg")));
pan.add(jlb);
this.setContentPane(jlb);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {Test t = new Test();}
}