总结:看下我的运行图片。这个图片很重要。很能说明问题。它的frame就是一个小图片。就是背景。么手贱把它放大。
在微软的操作系统上,你放多大,窗口就有多大,你看到背景就成了小图片,就会误以为不是背景。
package com.bc;
//添加被背景图片
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class yut extends JFrame {
public static void main(String[] args) {
yut y = new yut();
// y.getContentPane().setBackground(Color.red);
// y.setVisible(true);
}
public yut() {
JFrame frame = new JFrame("背景图片");
ImageIcon ico = new ImageIcon("D:\\21.jpg");
JLabel imagelabel = new JLabel(ico);
imagelabel.setBounds(0, 0, ico.getIconWidth(), ico.getIconHeight());
// 把内容窗格转换为JPanel,否则不能用setOpadut方法透明
JPanel imagepanel = (JPanel) frame.getContentPane();// 将背景图排脓放在标签里
imagepanel.setOpaque(false);
imagepanel.setLayout(new FlowLayout());// panel默认的布局管理器为:FlowLayout()
imagepanel.add(new JButton("yes"));
frame.getLayeredPane().setLayout(null);
frame.getLayeredPane().add(imagelabel, new Integer(Integer.MIN_VALUE));// 你妹这次对了,表示iamgelabel.永远是这个容器的最后一个组件
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 也就是说,imagelabel永远在getlayeredpane的最底层
frame.setSize(ico.getIconWidth(), ico.getIconHeight());
// frame.setSize(544, 555);
frame.setVisible(true);
}
}