展开全部
//不加包,图片跟类文件在62616964757a686964616fe4b893e5b19e31333262383634一个目录,命令行下编译执行就行了
//如果建工程,图片放到工程根目录
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
publicclassStudentextendsJFrame{
publicStudent(){
init();
}
privatevoidinit(){
finalContainerc=getContentPane();
finalJLabelimgLabel=newJLabel();
JButtonbtnPic=newJButton("换背景图片");
btnPic.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
ImageIconimg=newImageIcon(System.getProperty("user.dir")+"\\a.png");
imgLabel.setIcon(img);
imgLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
}
});
((JPanel)getContentPane()).setOpaque(false);
getLayeredPane().add(imgLabel,newInteger(Integer.MIN_VALUE));
setLayout(newFlowLayout());
add(btnPic);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newStudent().setVisible(true);
}