Jlabel 图片 / ImageIcon

Jlabel image / ImageIcon

我正在尝试向这些标签添加一些图像,但 Eclipse 向我抛出 java.lang.NullPointerException,我不知道为什么?!请帮忙:}

public class LabelPanel extends JPanel {                

      public JLabel[] bills;                        

      public LabelPanel() {                  
           setLayout(new FlowLayout());                                                                   
           init();               
           labelOrder();            
      }                         


      private void init() {                 
          bills = new JLabel[4];                    
           for (int i = 0; i < bills.length; i++){                                                       
               bills[0].setIcon(newImageIcon("C:\users\Acer\Documents\images\1.jpg"));                                 
               bills[1].setIcon(new ImageIcon("C:\Users\Acer\Documents\images\2.jpg"));                        
               bills[2].setIcon(new ImageIcon("C:\Users\Acer\Documents\images\5.jpg"));                        
               bills[3].setIcon(new ImageIcon("C:\Users\Acer\Documents\images\10.jpg"));                       
               bills[4].setIcon(new ImageIcon("C:\Users\Acer\Documents\images\20.jpg"));                   
           }                            
       }    

      private void labelOrder() {                               
           add(bills[0]);               
           add(bills[1]);               
           add(bills[2]);               
           add(bills[3]);               
           add(bills[4]);               
       }  
  }

试试下面的代码

示例代码

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;

    public class SwingExampleDemo {  
      public static void main(String[] args) {  
          JFrame f=new JFrame();//creating instance of JFrame 
          ImageIcon imageIcon = new ImageIcon("C:\users\Acer\Documents\images\1.jpg"); //here image path 
          JLabel jLabel = new JLabel(imageIcon);    
          f.add(jLabel);         
          f.setLayout(new FlowLayout());//using layout managers  
          f.setVisible(true);//making the frame visible  
          f.pack();
       }  
    }

希望对你有所帮助...