package com.instantcommunication.gui;import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class Login extends JFrame implements ActionListener {

   /**
 * 
 */
   private static final long serialVersionUID = 1L;
   private JPanel l_jp=null;
   private JLabel l_nameLabel=null,l_passLabel=null,l_image=null;  
   private JTextField l_jtf=null;
   private JPasswordField l_jpf=null;
   private JButton l_jb=null;
   private ImageIcon icon=new ImageIcon("a.jpg");
   Container con=this.getLayeredPane();
   Container con2=this.getContentPane();
   
   public Login(){
   setTitle("登录");
   l_jp=new JPanel(null);
   l_nameLabel=new JLabel("用户名");
   l_passLabel=new JLabel("密    码");
   l_image=new JLabel(icon);
   l_jtf=new JTextField(10);
   l_jpf=new JPasswordField(10);
   l_jb=new JButton("登录");   
   this.setBounds(200, 200, 380, 280);
   l_nameLabel.setBounds(70,60,50,30);
   l_passLabel.setBounds(70,110,50,30);
   l_jtf.setBounds(120, 60, 180, 30);
   l_jpf.setBounds(120, 110, 180, 30);
   l_jb.setBounds(150,170,80,30);
   l_image.setBounds(0, 0, 380, 280);
   con.add(l_image);
   con2.add(l_nameLabel);
   con2.add(l_passLabel);
   con2.add(l_jtf);
   con2.add(l_jpf);
   con2.add(l_jb);
   this.add(l_jp);
   l_jb.addActionListener(this);
   this.setVisible(true);
   this.setResizable(false);
   this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);  
   //repaint();
   }
  
   
   public void actionPerformed(ActionEvent e){
   if(e.getSource().equals(l_jb)){
   setVisible(false);
   }
   }
   
   public static void main(String[] args){
   new Login();
   }
}

解决方案 »

  1.   


    这里的两个Container实际上指的都是当前的容器。
    因此下面的用法:就相当于this和con,con2是等价的。
    也就是说,你在往一个容器里面添加组件,当然会出现覆盖。
    还有你要用"a.jpg"做背景的话,其实是分层排放的,你没有设置l_image放置在更下的一层。看似你分层了,其实没有。可以使用:con..add(l_image,new Integer(Integer.MIN_VALUE));来将l_image放置在最底层。
    还有你要设置你顶层的组件属性为透明,不然底层会被顶层覆盖。
      

  2.   


       con.add(l_image);  
    改成
    con.add(l_image, new Integer(Integer.MIN_VALUE));
       l_image.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());试试看。
      

  3.   

    怎么设置透明度呢?如果是重写paint方法或者painComponent方法又是怎样的代码?那个好?
      

  4.   

    我记得有个setOpaque()方法,你试试。。
    至于paint和painComponent方法的好处吗?
    painComponent只存在于swing中,awt中没有此方法。painComponent指的的是容器本身自己画出自己的组件;
    paint方法swing和awt中都有此方法。
    paint在swing中:改变容器本身的组件;
    paint在awt中:改变组件样式。
    说的有可能不对,要是详细的话,还是自己去查查的好!
      

  5.   

    我感觉你应该先把JFrame的border布局清空一下
    this.setLayout(null);
      

  6.   

    //得到顶级容器
    Container c=this.getContentPane();
    //设置为空布局
    this.setLayout(null);//背景图片必须放在最下面 
    BacImage bi=new BacImage();
    ///设置图片位置
    bi.setBounds(0, 0, 360, 360);
    c.add(bi);//背景图片内部类
    class BacImage extends JPanel
    { private static final long serialVersionUID = 1L;

    Image im=null;
    public BacImage()
    {
    im=new ImageIcon("image/login.gif").getImage();

    }
    //画图片函数
    public void paintComponent(Graphics g)
    {

    g.drawImage(im, 0, 0, 360, 360, this);
    }
    }
      

  7.   

    放一个我正在用的方法,可以在背景图片上放置任何控件。
    public static void asBackground(JFrame frame, String path) {
    frame.getContentPane().setLayout(null);
    ((JPanel) frame.getContentPane()).setOpaque(false);
    ImageIcon img = new ImageIcon(path); // 添加图片
    JLabel lblPicture = new JLabel(img);
    frame.getLayeredPane().add(lblPicture, new Integer(Integer.MIN_VALUE));
    lblPicture.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    frame.setBounds(300, 50, img.getIconWidth() + 5,
    img.getIconHeight() + 25);
    }
    直接调用这个方法,frame是你当前的JFrame,path是你图片的路径,绝对路径或者相对路径都可以。
    效果图:
      

  8.   

    为什么你contentpanel设置不透明?