6.21 在panel中设置背景并不覆盖控件--paintComponent

Posted -ifrush

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6.21 在panel中设置背景并不覆盖控件--paintComponent相关的知识,希望对你有一定的参考价值。

做java小游戏课设时需要在游戏主界面设置背景图片,最初参考的是这个博客:

https://blog.csdn.net/he13733625052/article/details/90177674

但是其是为Frame设置背景:

通过LayeredPane来为背景和控件进行分层,背景图片用为Label设置Imageicon实现,通过Frame.setLayeredPane(Pane)来为窗口设置背景。

而我是想为JPanel设置背景,而不是Frame,这时对JPanel的paintComponent进行重写就能实现不覆盖控件的重绘(而重写paint会覆盖控件)

    protected void paintComponent(Graphics g) {  
        g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this);  
    }  

 

以上是关于6.21 在panel中设置背景并不覆盖控件--paintComponent的主要内容,如果未能解决你的问题,请参考以下文章

如何在winfrom中设置panel为半透明颜色,可以看到panel下面的控件,也可以设置panel的颜色。

在 UWP 类库中设置用户控件的样式并在使用应用程序中覆盖它

在 Xamarin Android 中设置布局背景 - 覆盖暗模式

怎么样在vb中设置statusbar?

如何在 Qt 中设置按钮背景颜色,甚至覆盖平台主题?

Android中设置控件的背景颜色的方式整理