带有ImageIcon的Java For循环

带有ImageIcon的Java For循环,java,image,loops,for-loop,nullpointerexception,Java,Image,Loops,For Loop,Nullpointerexception,谁能帮我解决这个问题 imagePanel = new JPanel(); label = new JLabel(); for (int i = 0; i < 9; i++) { label.setIcon(image[i]); imagePanel.add(label); } 我所要做的就是在数组中循环,在循环过程中设置并显示图像 下面是使其更容易修复的整个程序: private ImageIcon image[] = { new Ima

谁能帮我解决这个问题

imagePanel = new JPanel();
label = new JLabel();

for (int i = 0; i < 9; i++)
{
   label.setIcon(image[i]);
   imagePanel.add(label);
}
我所要做的就是在数组中循环,在循环过程中设置并显示图像

下面是使其更容易修复的整个程序:

    private ImageIcon image[] = 
    {
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\1.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\2.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\3.jpg"),
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\4.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\5.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\6.jpg"),
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\7.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\8.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\9.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\10.jpg") 
    };

    private JLabel label;

    private JLabel sofaImageLabel;
    private JLabel armchairImageLabel;
    private JLabel cDeskImageLabel;
    private JLabel cTableImageLabel;
    private JLabel tvStandImageLabel;
    private JLabel cushionImageLabel;
    private JLabel bedImageLabel;
    private JLabel mattressImageLabel;
    private JLabel duvetImageLabel;
    private JLabel pillowImageLabel;

    private JPanel buttonPanel;
    private JPanel imagePanel;

    private JButton button;

    public Furniture_System()
    {
        setTitle("Furniture Management System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        buildImagePanel();
        // buildButtonPanel();

        add(imagePanel, BorderLayout.WEST);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setVisible(true);   
    }

    private void buildImagePanel()
    {
        imagePanel = new JPanel();

        for (int i = 0; i < 9; i++)
        {
            label = new JLabel();
            label.setIcon(image[i]);
            imagePanel.add(label);
        }
    }

       private void buildButtonPanel()
       {
          buttonPanel = new JPanel();

          button = new JButton("Get Image");

          button.addActionListener(new chooseFileButton());
          buttonPanel.add(button);
       }

       private class chooseFileButton implements ActionListener
       {

          public void actionPerformed(ActionEvent e)
          {
        /*    String filename = null;

            JFileChooser fileChosen = new JFileChooser();
            int status = fileChosen.showOpenDialog(null);

            if (status == JFileChooser.APPROVE_OPTION)
            {
                File selectedFile = fileChosen.getSelectedFile();
                filename = selectedFile.getPath();
                JOptionPane.showMessageDialog(null, "You selected "  + filename);
            }

            ImageIcon image = new ImageIcon(filename);
            sofaImageLabel.setIcon(image);
            sofaImageLabel.setText(null);
            pack(); */
          }
       }

    public static void main(String[] args)
       {
          new Furniture_System();
       }

}
私有图像图标图像[]=
{
新图像图标(“C:\\Users\\James\\Pictures\\Java\\1.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\2.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\3.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\4.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\5.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\6.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\7.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\8.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\9.jpg”),
新图像图标(“C:\\Users\\James\\Pictures\\Java\\10.jpg”)
};
私人标签;
私人JLabel sofaImageLabel;
私人JLabel标签;
私人JLabel爱斯基摩标签;
专用JLabel-cTableImageLabel;
私人JLabel标签;
私人JLabel标签;
私人JLabel标签;
私人JLabel床垫标签;
私人JLabel羽绒被标签;
私人JLabel枕套标签;
私人JPanel按钮面板;
私人JPanel imagePanel;
私人按钮;
公共家具系统()
{
setTitle(“家具管理系统”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(新的BorderLayout());
buildImagePanel();
//buildButtonPanel();
添加(imagePanel,BorderLayout.WEST);
添加(按钮面板,边界布局。南部);
包装();
setVisible(真);
}
私有void buildImagePanel()
{
imagePanel=newjpanel();
对于(int i=0;i<9;i++)
{
label=新的JLabel();
label.setIcon(图像[i]);
imagePanel.add(标签);
}
}
私有void buildButtonPanel()
{
buttonPanel=新的JPanel();
按钮=新的JButton(“获取图像”);
addActionListener(新建chooseFileButton());
按钮面板。添加(按钮);
}
私有类chooseFileButton实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
/*字符串文件名=null;
JFileChooser fileselected=新的JFileChooser();
int status=fileselected.showOpenDialog(null);
if(status==JFileChooser.APPROVE\u选项)
{
File selectedFile=fileselected.getSelectedFile();
filename=selectedFile.getPath();
showMessageDialog(null,“您选择了”+文件名);
}
ImageIcon image=新的ImageIcon(文件名);
sofaImageLabel.setIcon(图像);
sofaImageLabel.setText(空);
包装()*/
}
}
公共静态void main(字符串[]args)
{
新家具系统();
}
}

任何组件只能有一个父容器,不能重复使用。 因此,代码必须是:

for (ImageIcon img : image)
{
    JLabel label = new JLabel();
    label.setIcon(img);
    imagePanel.add(label);
}

尽管我会在Furniture_System.java的第55行(在构造函数中)中查看对null的访问。如上所述,可能
imagePanel
为空。

我还看不出NPE的原因,但您似乎多次将相同的JLabel添加到GUI中,这是行不通的。您只能向GUI添加一次组件。也许您想在for循环中创建一个新的JLabel。在
Furniture\u System.java
文件中,第113行和第55行的内容是什么?
buttonPanel
可能是空的。您注释掉了创建按钮面板的方法调用,
buildButtonPanel(),这导致了您的NPE,然后在您最初的问题中,向我们展示了与问题完全无关的代码。因为您需要查看NPE指向您的行。是的,修复了它,我没有意识到,因为我没有使用它!谢谢,你可以点击左边的按钮来接受这个答案。
    private ImageIcon image[] = 
    {
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\1.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\2.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\3.jpg"),
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\4.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\5.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\6.jpg"),
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\7.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\8.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\9.jpg"), 
        new ImageIcon ("C:\\Users\\James\\Pictures\\Java\\10.jpg") 
    };

    private JLabel label;

    private JLabel sofaImageLabel;
    private JLabel armchairImageLabel;
    private JLabel cDeskImageLabel;
    private JLabel cTableImageLabel;
    private JLabel tvStandImageLabel;
    private JLabel cushionImageLabel;
    private JLabel bedImageLabel;
    private JLabel mattressImageLabel;
    private JLabel duvetImageLabel;
    private JLabel pillowImageLabel;

    private JPanel buttonPanel;
    private JPanel imagePanel;

    private JButton button;

    public Furniture_System()
    {
        setTitle("Furniture Management System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        buildImagePanel();
        // buildButtonPanel();

        add(imagePanel, BorderLayout.WEST);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setVisible(true);   
    }

    private void buildImagePanel()
    {
        imagePanel = new JPanel();

        for (int i = 0; i < 9; i++)
        {
            label = new JLabel();
            label.setIcon(image[i]);
            imagePanel.add(label);
        }
    }

       private void buildButtonPanel()
       {
          buttonPanel = new JPanel();

          button = new JButton("Get Image");

          button.addActionListener(new chooseFileButton());
          buttonPanel.add(button);
       }

       private class chooseFileButton implements ActionListener
       {

          public void actionPerformed(ActionEvent e)
          {
        /*    String filename = null;

            JFileChooser fileChosen = new JFileChooser();
            int status = fileChosen.showOpenDialog(null);

            if (status == JFileChooser.APPROVE_OPTION)
            {
                File selectedFile = fileChosen.getSelectedFile();
                filename = selectedFile.getPath();
                JOptionPane.showMessageDialog(null, "You selected "  + filename);
            }

            ImageIcon image = new ImageIcon(filename);
            sofaImageLabel.setIcon(image);
            sofaImageLabel.setText(null);
            pack(); */
          }
       }

    public static void main(String[] args)
       {
          new Furniture_System();
       }

}
for (ImageIcon img : image)
{
    JLabel label = new JLabel();
    label.setIcon(img);
    imagePanel.add(label);
}