背景圖片需要放在JFrame的JLayeredPane層中,而且需要把覆蓋在其上面的JPanel設成透明,才能作為背景被顯示出來.我給妳個例子,妳看看吧.
import?java.awt.BorderLayout;import?javax.swing.ImageIcon;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JLayeredPane;
import?javax.swing.JPanel;
public?class?BackGroundPicture?extends?JFrame{
JButton?jb=new?JButton("打印"); ImageIcon?image=new?ImageIcon("Penguins.jpg");//這裏背景圖片文件換成妳的背景圖片文件 JLabel?jl=new?JLabel(image);//添加背景圖片 JPanel?jp?=?new?JPanel(); public?BackGroundPicture(){ super("圖片打印窗口"); jp.add(jb); jp.setOpaque(false); JLayeredPane?jlp=this.getLayeredPane(); jlp.setLayout(new?BorderLayout()); jlp.add(jl);this.add(jp);
this.setSize(800,?600); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); this.setResizable(true); this.setVisible(true); } public?static?void?main(String[]?args)?{ new?BackGroundPicture(); }}