public class beijing2 extends JFrame {
....
.......
beijing2(){
this.setContentPane(new MyPanel());//调用MyPanel
........
..............
}
public static void main(String[] args) {
new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
把整个图片加在标签上,然后把标签的大小和窗口一样也可以充当背景图片//如果不能显示jpg的图片就把jpg图片转化为png格式的