首页 > 解决方案 > How to add a background Image on JPanel without extending?

问题描述

I want to add a background image on the panel. Most of the answers here says that you should use JLabel or extend a JPanel to use the paintComponent.

However in my project I used IntelliJ IDEA, with its GUI builder. Through the GUI builder I made my class extend through JFrame. Hence I can't use the extend panel method.

Here's how I have my class with its extending frame.

public class StoryFrame extends JFrame {
    private JPanel mainPanel;
    public StoryFrame(){
        this.setContentPane(mainPanel);
    }
}

I want to have mainPanel have its own background image.

I used a GridLayout so overlapping Swing Components also isn't an option.

Any other methods on how can we add a background image on a panel?

标签: javaimageswingbackgroundjpanel

解决方案


推荐阅读