为什么在Container中无法插入图片
         lab=new JLabel("姓名");
txt_name=new JTextField(10); Container con=this.getContentPane();

ImageIcon img=new ImageIcon("D:\\oemlogo.jpg");

部分代码是这样的,。我是像要用这图片当做背景  oemlogo.jpg 
 然后显示出标签lab  和文本 txt_name

解决方案 »

  1.   

    把背景图片先加到jpanel上,再把jpanel添加到con就行了.
      

  2.   

    jpanel
    具体用什么方法添加图片,。。
      

  3.   

    这是我用jlabel写的,也是前几天在这里学会的,现在发给你,呵呵,我也是新手!
    //
    ImageIcon shopicon =new ImageIcon(Friend.class.getResource("/images/main/alert/shop.png"));
    JLabel bgView;//
    Container icontentPane = interframe.getContentPane();
    bgView = new JLabel(shopicon);         
    icontentPane.add(bgView); 参考一下吧,我水平也很差的!
      

  4.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class NewLabel extends JFrame{
    private JLabel lab;
    private JLabel lab2;
    private JTextField txt_name; public NewLabel()
    {
    Container con=this.getContentPane();
    ImageIcon img=new ImageIcon("D:\\oemlogo.jpg");

    JLabel lab = new JLabel(img);
     
    con.add(lab);


    this.setSize(300,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    }
     
    public static void main(String [] args)
    {
    new NewLabel();
    }
    }
    我的完整代码是这样的,。。但是最后却还是没有图片出来
      

  5.   

    我改了一下你的代码,你看一下,你的代码里lab经以声明了,不用在构造方法里再JLabel lab = new JLabel(img);
    直接写 lab = new JLabel(img); 就行了.import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; public class Teat extends JFrame{ 
    private JLabel lab; 
    private JLabel lab2; 
    ImageIcon img;
    private JTextField txt_name;  public Teat() 

    Container con=this.getContentPane(); 
    img =new ImageIcon(Teat.class.getResource("/images/main/alert/shop.png"));   lab = new JLabel(img);  con.add(lab); 
    this.setSize(300,200); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setVisible(true); 
    }  public static void main(String [] args) 

    new Teat();