super("《《 日 历 》》");
jlb=new JLabel(new ImageIcon("bg.jpg"));
this.getLayeredPane().add(jlb);
((JPanel)getContentPane()).setOpaque(false);
this.getContentPane().setLayout(new BorderLayout());
st=new ShowTimePanel();
tc=new TextCalendar();
tc.setCalendar();
this.getContentPane().add(tc,"Center");
this.getContentPane().add(st,"South");
setVisible(true);
setSize(400,250);
jlb=new JLabel(new ImageIcon("bg.jpg"));
this.getLayeredPane().add(jlb);
((JPanel)getContentPane()).setOpaque(false);
this.getContentPane().setLayout(new BorderLayout());
st=new ShowTimePanel();
tc=new TextCalendar();
tc.setCalendar();
this.getContentPane().add(tc,"Center");
this.getContentPane().add(st,"South");
setVisible(true);
setSize(400,250);
import java.awt.Graphics;
public class BackImg extends JFrame{
public BackImg(){
super("背景图片");
this.setSize(500,400);
JPanel p=new JPanel(){
public void paintComponent(Graphics g){
ImageIcon img=new ImageIcon("F:/a.jpg");
g.drawImage(img.getImage(),0,0,getSize().width,getSize().height,null);
}
};
p.add(new JButton("背景图片"));
p.add(new JLabel("背景图片"));
this.setContentPane(p);
setVisible(true) ;
}
public static void main (String[] args) {
new BackImg();
}
}
将jlb=new JLabel(new ImageIcon("bg.jpg")); 代码改为
ImageIcon image = new ImageIcon(this.getClass().getResource("bg.jpg");
jlb=new JLabel(image); 如果图片是单独放在一个文件夹可以再调整一下路径,路径的开始不用加"/"
其实重写这个方法就行
public void paint(Graphics g){
ImageIcon img=new ImageIcon("F:/a.jpg");
g.drawImage(img.getImage(),0,0,getSize().width,getSize().height,null);
}