主题:添加背景图片
有时候想给背景添加图片,可是只显示了图片的一部分,是不是很纳闷啊!那就看看下面的程序吧!
java代码
import java.awt.*;
import javax.swing.*;
public class Samp extends JFrame{
public static void main(String[] args){
new Samp();
}
public Samp(){
Image image = new ImageIcon("../pic/ok.png").getImage();// 这是背景图片 .png .jpg .gif 等格式的图片都可以
JLabel imgLabel = new aLabel(image);// 将背景图放在"标签"里。
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));// 注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。
Container cp = this.getContentPane();
((JPanel) cp).setOpaque(false); // 注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。
this.setLocation(200,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setResizable(false);