JLabel lab=new JLabel(new ImageIcon("login/welcome.jpg"));
lab.setBounds(20,20,100,100);
add(lab);
为什么不显示呢?
把Icon对象换成文字就正常我得图片保存在login(包名) /  welcome.jpg   与源文件在一起   哪里的问题?

解决方案 »

  1.   

    背景用jpanel啊,怎么用标签呀
      

  2.   

    key word: JFrame JTextArea 背景 图片   background image
    最近一直在忙着一个HCI的项目,我们小组做的是一个食谱,在实现功能的同时,我们需要在街面上美观,这就需要在背景中添加合适的图片来美化。开始的时候,我是巴图片迁入label中,然后平铺开来。结果是:其他覆盖在他上面的组件无法很好的显示,需要鼠标点到那个组建才会显示出来,这样的效果显然不能满足我们的HCI的要求。
    所以下面介绍一种比较好,可行的方法:
    把上面所说的label改成组件JPanel.
     
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    public class BackgroundImage extends JFrame
    {
        JScrollPane scrollPane;
        ImageIcon icon;
        Image image;
     
        public BackgroundImage()
        {
            icon = new ImageIcon("bgpanel.jpg");
     
            JPanel panel = new JPanel()
            {
                protected void paintComponent(Graphics g)
                {
                    // Dispaly image at at full size                g.drawImage(icon.getImage(), 0, 0, null);
     
                    // Scale image to size of component//                Dimension d = getSize();//                g.drawImage(icon.getImage(), 0, 0, d.width, d.height, null); 
                    // Fix the image position in the scroll pane//                Point p = scrollPane.getViewport().getViewPosition();//                g.drawImage(icon.getImage(), p.x, p.y, null); 
                    super.paintComponent(g);
                }
            };
            panel.setOpaque( false );
            panel.setPreferredSize( new Dimension(400, 400) );
            scrollPane = new JScrollPane( panel );
            getContentPane().add( scrollPane );
     
            JButton button = new JButton( "Hello" );
            panel.add( button );
        }
     
        public static void main(String [] args)
        {
            BackgroundImage frame = new BackgroundImage();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 300);
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    http://blog.chinaunix.net/u/21684/showart_258857.html
      

  3.   


    路径的问题,执行的时候没有 bin 这个路径的。new ImageIcon(getClass().getResource("login/welcome.jpg"))
      

  4.   

    是这样的
    JLabel默认是透明的
    你需要把它setOpaque(true)
      

  5.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class login1  extends JFrame {
    protected JLabel label;
    public login1(){

    setTitle("ttt");
    setSize(244,180);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container cp = getContentPane();
    ImageIcon icon = new ImageIcon("bj3.jpg");
    label = new JLabel(icon);
    cp.add(label);
    }
    public static void main (String[] args){
    login1 cy = new login1();



    cy.setVisible(true);
    }
      

  6.   

    import java.awt.*;
    import javax.swing.*;
    public class Fbackground
    {
    public static void main(String [] args)
    {
    new Fb("带有背景图片的窗体");
    }}class Fb extends JFrame
    {
    JPanel pane=new JPanel();
    final Image img = Toolkit.getDefaultToolkit().createImage("a.jpg");   //图片a.jpg位于根目录
    Button b=new Button("一个按钮");
    public Fb(String title)
    {
    this.setTitle(title);
    this.setBounds(200,200,400,400);
    this.setLayout(new FlowLayout());
    getContentPane().add(pane,BorderLayout.CENTER); 
    setIconImage(img);
    this.add(b);
    this.setVisible(true);
    } public void paint(Graphics g)
    {
    g.drawImage(img,0,0,this);
    }
    }
    toolkit是awt包里的
    getDefaultToolkit()是toolkit类的获取默认工具包,静态方法
    createImage("a.jpg")建立Image类型的变量