public class SGL {
public static void main(String[] args) {
new SGL();
} JLabel lable;// 定义静态文本
JTextArea jta = new JTextArea(10, 44);
// 设置按钮上显示的图标
// 按钮图标
ImageIcon baiduimage = new ImageIcon("F:\\MyEclipse\\编程思想\\BD.jpg"),
googleimage = new ImageIcon("F:\\MyEclipse\\编程思想\\GG.jpg"),
taobaoimage = new ImageIcon("F:\\MyEclipse\\编程思想\\TB.jpg"),
csdnimage = new ImageIcon("F:\\MyEclipse\\编程思想\\BD.jpg"),
yjimage = new ImageIcon("F:\\MyEclipse\\编程思想\\YJ.jpg");
// 定义按钮
JButton baiduButton;
JButton googleButton;
JButton taobaoButton;
JButton csdnButton;
JButton yjButton;
JButton qqkjButton;
JButton wkButton;
JButton qqyyButton;
JButton yyButton;
JButton qqxfButton;
JButton qqButton;
JButton nkButton;
JButton eclButton;
JButton dngjButton;
JButton rjglButton;
JButton xlkkButton;
JButton dztButton;
JButton kgButton;
JButton ydButton;
JButton gsButton;
JButton bangzhuButton;
JButton tuichuButton;
JFrame frame; public SGL() {
// 定义静态文本的内容
JLabel lr = new JLabel(
"请在文本框内输入您要搜索的内容:                                                                                               ");
JLabel hp = new JLabel(
"帮助选择区:                                                                                                                                      ");
JLabel wz = new JLabel(
"常用网址区:                                                                                                                                      ");
JLabel ss = new JLabel(
"搜索选择区:                                                                                                                                      ");
JLabel rj = new JLabel(
"常用软件集成区:                                                                                                                              ");
JLabel wl = new JLabel(
"欢迎来到常用软件集成中心!                                                                                                           ");
JLabel ts = new JLabel(
"如果您需要多次操作,请注意清空输入框!                                                                                 "); // 界面容器命名
frame = new JFrame("常用软件集成中心");
JPanel imagePanel;
// 设定窗口上显示的图标
Toolkit tk = Toolkit.getDefaultToolkit();
Image im = tk.getImage("F:\\MyEclipse\\编程思想\\MR.jpg");
frame.setIconImage(im);
// 按钮命名,添加背景色
baiduButton = new JButton("百度搜索", baiduimage);
baiduButton.setBackground(Color.pink); googleButton = new JButton("谷歌搜索", googleimage);
googleButton.setBackground(Color.pink); taobaoButton = new JButton("淘宝搜索", taobaoimage);
taobaoButton.setBackground(Color.pink); csdnButton = new JButton("CSDN论坛",yjimage); yjButton = new JButton("远景论坛"); wkButton = new JButton("百度文库"); qqkjButton = new JButton("Q  Q空间"); qqyyButton = new JButton("Q   Q音乐"); qqxfButton = new JButton("Q   Q旋风"); qqButton = new JButton("腾讯Q  Q"); yyButton = new JButton("Y   Y语音"); nkButton = new JButton("NetKeeper"); eclButton = new JButton("MyEclipse"); dngjButton = new JButton("电脑管家"); rjglButton = new JButton("软件管理"); xlkkButton = new JButton("迅雷看看"); dztButton = new JButton("金山打字"); kgButton = new JButton("酷狗音乐"); ydButton = new JButton("115网盘"); gsButton = new JButton("格式工厂"); bangzhuButton = new JButton("帮助文档"); tuichuButton = new JButton("退出程序"); // 布局用户界面
// 这是背景图片
ImageIcon img = new ImageIcon("F:\\MyEclipse\\编程思想\\BJ.jpg");
// 将背景图放在标签里
JLabel imgLabel = new JLabel(img);
// 设置背景标签的位置
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
imagePanel = (JPanel) frame.getContentPane();
imagePanel.setOpaque(false);
imagePanel.setLayout(new BorderLayout());
// 文本框
imagePanel.add(lr);
imagePanel.add(jta);

// 搜索选择区域
imagePanel.add(ss);
imagePanel.add(baiduButton);
imagePanel.add(googleButton);
imagePanel.add(taobaoButton);
// 常访问的网址区
imagePanel.add(wz);
imagePanel.add(csdnButton);
imagePanel.add(yjButton);
imagePanel.add(qqkjButton);
imagePanel.add(wkButton);
// 常用软件集成区域
imagePanel.add(rj);
imagePanel.add(qqyyButton);
imagePanel.add(yyButton);
imagePanel.add(qqxfButton);
imagePanel.add(qqButton);
imagePanel.add(nkButton);
imagePanel.add(eclButton);
imagePanel.add(dngjButton);
imagePanel.add(rjglButton);
imagePanel.add(xlkkButton);
imagePanel.add(dztButton);
imagePanel.add( kgButton);
imagePanel.add(ydButton);
imagePanel.add(gsButton);
// 帮助
imagePanel.add(hp);
imagePanel.add(bangzhuButton);
imagePanel.add(tuichuButton);
// 底部软件提示信息
imagePanel.add(wl);
imagePanel.add(ts);

frame.getLayeredPane().setLayout(null);
// 把背景图片添加到分层窗格的最底层作为背景
frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 界面大小
frame.setSize(img.getIconWidth(), img.getIconHeight());
// 设置窗口大小不可改变
frame.setResizable(false);
// 窗口周期和窗口事件监控
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
frame.dispose();
}
});
frame.setVisible(true);

解决方案 »

  1.   

    貌似背景图片不能这样添加吧。
    应该是重写paint(g)方法
    当然我不确定,因为我没有用过你那样的方式成功过。
    但尝试过,分层结构我是在了paint方法中实现的.
    http://bbs.csdn.net/topics/390280211
    连接时这个论坛一个人发的技术贴,介绍背景绘图的。
    另外补一句,如果只是单纯的想按钮显示在背景之上的话不用那么麻烦的,add组件的时候先添加buttion,后添加背景JLabel就可以了。
    引用:
    其实java想把界面做漂亮点的话,只要把背景做好就行了,在jdk1.6以后可以继承JFrame,或JWindow后如下设置即可 
    setUndecorated(true);//不要标题栏的修饰,主要防止一下代码抛异常,
    AWTUtilities.setWindowOpaque(this, false);   //关键点,设置窗体的透明,这种透明不会影响到内部组件,方便绘制背景。
    然后再自己重写一下JPanel的  paintComponent(Graphics g)方法,用该方法绘制背景,最好不要用paint方法,因为该方法会覆盖内部组件,遮住内部组件很蛋疼的哟。将自己写的JPanel通过setContentPane方法加入到JFrame或JWindow的contentPane中,再在contentPane里添加自己的组件即可。