在JFame中重载paint方法显示图片

先上显示效果:

代码:

package com.hy;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class ShowGui extends JFrame{
    
    public void paint(Graphics g){
        ImageIcon icon = new ImageIcon("c:/hy/2611.jpg");
        int iconWidth=icon.getIconWidth();
        int iconHeight=icon.getIconHeight();
        g.drawImage(icon.getImage(), (int) (this.getWidth() - iconWidth) / 2,
                (int) (this.getHeight() - iconHeight) / 2, iconWidth, iconHeight, null);
    }

}

End

posted @ 2021-08-05 13:08  逆火狂飙  阅读(114)  评论(0)    收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东