在JLabel上显示图像

时间:2017-03-14 07:10:12

标签: java swing jlabel

我在JLabel上显示图像时遇到问题。我有另一个名为ControlPanel的类,它将图像保存在项目文件夹中。我有在这个类中保存图像的方法,但由于某种原因,我得到了一个N​​ullPointerException。当我在其他课程中移动它们时,一切都开始正常工作。实际图像是用户绘制的bufferedImage。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GalleryPanel extends JPanel 
{

    private static final long serialVersionUID = 1L;
    private JLabel[] images;
    private static final int MAX_IMAGES = 12;
    private int currentImage;


    public void init()
    {   
        images = new JLabel[MAX_IMAGES];
        setLayout(new GridLayout(3,4));
        setBackground(Color.GRAY);
    }


    public void addImageToGallery(String fileName, String fileExtension)
    {
        if ( currentImage < images.length - 1)
        {   
            ImageIcon icon = new ImageIcon(fileName + fileExtension);

            images[currentImage] = new JLabel(icon, JLabel.CENTER);

            add(images[currentImage]);

            displayImage(currentImage);

            currentImage++;
        }
        else
        {
            throw new ArrayIndexOutOfBoundsException("The gallery is full");
        }
    }


    // display the doily image in the gallery
    public void displayImage(int index)
    {
        images[index].setSize(300, 300);
        add(images[index]);
    }


    public final int getMaxImages()
    {
        return MAX_IMAGES;
    }


    public Dimension getPreferredSize() 
    {
          return new Dimension(380, 700);
    }

}

这是我其他课程中负责保存实际图像的两种方法

// Shows a dialog box which asks the user to choose a name for the file that he wants to save
public void saveImage(BufferedImage bufImage)
{
    String fileName = JOptionPane.showInputDialog("Choose image name");

    if (fileName != null)
    {   
        if(fileName.equals(""))
        {
            fileName = "Untitled";
        }

        chooseImageFormat(bufImage, fileName);
    }   

}


    //shows a dialog box which asks the user to select the file format of the image he would like to save
public void chooseImageFormat(BufferedImage bufImage, String fileName)
{
    Object[] imageFormats = {"PNG", "JPEG"};

    String userInput = (String) JOptionPane.showInputDialog(null, "Choose file format", "File Format Settings", JOptionPane.PLAIN_MESSAGE, null, imageFormats, "PNG");


    String imageFormat = (userInput.equals("PNG")) ? "PNG" : "JPEG";
    String fileExtension = (imageFormat.equals("PNG")) ? ".png" : ".jpg";

    File file = new File(fileName + fileExtension );

    try 
    {
        ImageIO.write(bufImage, imageFormat, file);
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    gallery.addImageToGallery(fileName, fileExtension);
}

1 个答案:

答案 0 :(得分:0)

我想你宣布你的画廊是这样的:

GalleryPanel gallery;

为此,你得到NullPointerException,所以改为使用:

GalleryPanel gallery = new GalleryPanel();

修改

  

它有效,但有没有办法不像这样实例化画廊?

你应该在使用之前声明并初始化它,还有另一个解决方案,但是你应该对你的代码进行很多更改,你必须做:

public static void addImageToGallery(String fileName, String fileExtension) {

你可以像这样调用静态方法

GalleryPanel.addImageToGallery(fileName, fileExtension);

但就像我说的那样,你应该做出很多改变。

祝你好运。