第2个回答 2018-01-04
final JFrame jf=new JFrame("我的窗体");
JButton jb=new JButton("嘟嘟嘟");
//背景图片的路径。(相对路径或者绝对路径。)
String path = "E:\\优美图片\\123.jpg";
// 背景图片
ImageIcon background = new ImageIcon(path);
// 把背景图片显示在一个标签里面
JLabel label = new JLabel(background);
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0, 0, this.getWidth(), this.getHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
JPanel imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);
// 把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
label.add(jb);
jf.add(label); //将标签添加至窗体中