How can we create a main JFrame with background image and a JFrame inside the main JFrame with Java Swing?
-
2I have a [drinking problem](http://i51.photobucket.com/albums/f380/allelse/anythingelse/airplane1213112.jpg) – Matt Ball Apr 26 '10 at 21:27
-
1I think you mean unhelpful :P – Matt Ball Apr 26 '10 at 21:55
-
Repeat question for image background http://stackoverflow.com/questions/1466240/how-to-set-an-image-as-a-background-for-frame-in-swing-gui-of-java – Romain Hippeau Apr 26 '10 at 23:11
3 Answers
I believe you are looking for internal frames.
For the background image bit, sublass JPanel, override its paintComponent() method, and blit your image there. Then set an instance of that panel as your JFrame's content pane.
public class BackgroundPanel extends JPanel {
private BufferedImage bgImg;
public BackgroundPanel() {
try {
bgImg = ImageIO.read(BackgroundPanel.class.getResourceAsStream(
"mybackgroundimage.png"));
} catch (IOException ex) {
System.err.println("Could not load background image!");
ex.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bgImg != null) {
g.drawImage(bgImg, 0, 0, null);
}
}
}
public class MyJFrame extends JFrame {
public MyJFrame() {
setContentPane(new BackgroundPanel());
}
}

- 3,883
- 4
- 31
- 29
It is hard to know what you are meaning with
a JFrame inside the main JFrame
Have a read about what a JFrame really is. Maybe you want a dialog window in your application, or maybe an internal window. Or maybe just another panel.
To get an background image in a JFrame, I recommend that you simply add a JPanel with a backround image to the JFrame:s contentpane.

- 121,568
- 97
- 310
- 388
Based on my understanding, you won't need to nest a JFrame inside another JFrame and I don't think it is good design to do so too. What you can do is nest JPanels instead.
You will mainly need to know about these two classes:

- 11,901
- 4
- 26
- 30