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

如何添加一张背景图片在Frame中?
硬盘上存有一张图片,假设其路径是"F:\\image.jpg",如何把这个图片设置为背景图片
背景图片

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

public class Hello extends JPanel {
    private Image backgroundImage;

    public Hello() {
        String path = "/Users/Biao/Desktop/x.png";
        backgroundImage = new ImageIcon(path).getImage();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
    }

    private static void createGuiAndShow() {
        JFrame frame = new JFrame("");

        frame.getContentPane().add(new Hello());

        // Set frame's close operation and location in the screen.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createGuiAndShow();
    }
}


------解决方案--------------------
楼主这些个问题可以直接百度
GOOGLE得到答案的结果比单纯在这上面问
快的多的