- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add background Image to JFrame in Java
To add background image to JFrame, use the getImage() method of the Image class −
Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg");
Now, draw the image −
public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); }
The following is an example to add Background Image to JFrame −
Example
import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.IOException; import javax.swing.JPanel; public class SwingDemo extends javax.swing.JFrame { Image img = Toolkit.getDefaultToolkit().getImage("E:\rahul.jpg"); public SwingDemo() throws IOException { this.setContentPane(new JPanel() { @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(img, 0, 0, null); } }); pack(); setVisible(true); } public static void main(String[] args) throws Exception { new SwingDemo(); } }
Output
- Related Articles
- How to change JFrame background color in Java
- How to add image as background into chart in Excel?
- How to maximize JFrame in Java Swing
- How to set FlowLayout for JFrame in Java?
- How to activate and deactivate JFrame in Java
- How to add background music in HTML?
- How to set default button for JFrame in Java?
- How to set background image in CSS using jQuery?
- How to add noise to an image using Java OpenCV library?
- How to add text to an image using Java OpenCV library?
- How to add borders to an image using Java OpenCV library?
- How to disable close button on a JFrame in Java?
- How to create a Titleless and Borderless JFrame in Java?
- How to set background image of a webpage?
- How to change the background image using jQuery?

Advertisements