- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements