import java.awt.*;
import javax.swing.*;
public class TestBackgroundColor extends JFrame? {
public static void main(String[] args)? {
TestBackgroundColor tbc = new TestBackgroundColor();
tbc.setVisible(true);
}
private JPanel imagePanel;
private ImageIcon background;
public TestBackgroundColor()? {
background = new ImageIcon("項目背景圖片
JLabel label = new JLabel(background);//把背景圖片顯示在一個標簽里面
//把標簽的大小位置設置為圖片剛好填充整個面板
label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());
//把內(nèi)容窗格轉(zhuǎn)化為JPanel,否則不能用方法setOpaque()來使內(nèi)容窗格透明
imagePanel = (JPanel)this.getContentPane();
imagePanel.setOpaque(false);
//內(nèi)容窗格默認的布局管理器為BorderLayout
imagePanel.setLayout(new FlowLayout());
imagePanel.add(new JButton("測試按鈕"));
this.getLayeredPane().setLayout(null);
//把背景圖片添加到分層窗格的最底層作為背景
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(background.getIconWidth(),background.getIconHeight());
this.setVisible(true);
}
}