日期:2014-05-20  浏览次数:20808 次

如何在JFrame或JPanel里添加背景图片
我的问题在标题里已经说得很清楚了,有谁知道?

------解决方案--------------------
Java code
import java.awt.*;
import javax.swing.*;

class ImagePanel extends JPanel {
    private Image backgroundImage;
    ImagePanel() {
        // 从系统中加载图片
        backgroundImage = new ImageIcon("image address").getImage();
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        }
    }
}