我在jframe里设置了一个图片背景  前面的控件就都没有了 被覆盖了  我不要覆盖 我要拿它做背景  呜呜

解决方案 »

  1.   

    重写窗体的printfComponent方法。
      

  2.   

    import javax.swing.*;
    import java.awt.*;
    public class BackgrounDemo extends JFrame { public BackgrounDemo() {
    init();
    }
    private void init() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(300,300);   ImageIcon img = new ImageIcon(BackgrounDemo.class.getResource("1.jpg"));
      JLabel bgLabel = new JLabel(img);
      this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));
      bgLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());   this.getContentPane().add(new JLabel("Hello Background"));
      ((JPanel)getContentPane()).setOpaque(false);

    }
    public static void main(String[] args) {
    BackgrounDemo bd=new BackgrounDemo();
    bd.setVisible(true);
    }}
    lz给你个例子希望能帮到你,这个例子能实现你想要的效果,本人亲自运行过!!!
      

  3.   


    楼上的是在JLabel中加背景啊,
    可以是可以,但楼主要注意改变窗口大小时要改变背景的大小。
      

  4.   


    确实,这不是最好的解决方案,如果楼主想更好的解决方案就直接向1楼说的重写窗体的printfComponent方法。