// 创建主窗口
JFrame frame = new JFrame("xxxxx");
frame.setSize(1200, 700);
frame.setLocation(350, 200);
JLabel label = new JLabel();
//新建一个类
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class PictureJPanel extends JPanel{
private ImageIcon icon;
private Image image;
public PictureJPanel() {
icon = new ImageIcon(getClass().getResource("/bg.jpg"));
image = icon.getImage();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//下面这行是为了背景图片可以跟随窗口自行调整大小,可以自己设置成固定大小
g.drawImage(image, 0, 0,this.getWidth(), this.getHeight(), this);
}
}
//主类里 背景图片插入
PictureJPanel pj = new PictureJPanel();
pj.repaint();
pj.setSize(1200, 675);
pj.setLocation(0, 0);
frame.repaint();
frame.add(pj);