Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拖放;将图像从文件夹拖放到JTextPane_Java_Swing_Drag And Drop_Wysiwyg - Fatal编程技术网

Java 拖放;将图像从文件夹拖放到JTextPane

Java 拖放;将图像从文件夹拖放到JTextPane,java,swing,drag-and-drop,wysiwyg,Java,Swing,Drag And Drop,Wysiwyg,我想实现一个JTextPane来编辑文本,使用从系统拖放到JTextPane中的图像。我试图通过使用JTextPane作为超类和DropTargetListener作为接口来实现它。但是当我试图读取我的输入流时,我得到了一个NullPointerException。请帮忙 import java.awt.datatransfer.*; import java.awt.dnd.*; import java.io.*; import javax.imageio.ImageIO; import ja

我想实现一个
JTextPane
来编辑文本,使用从系统拖放到
JTextPane
中的图像。我试图通过使用
JTextPane
作为超类和
DropTargetListener
作为接口来实现它。但是当我试图读取我的输入流时,我得到了一个
NullPointerException
。请帮忙

import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;

public class JTextTest extends JTextPane implements DropTargetListener {

private static final long serialVersionUID = 1L;

public JTextTest() {
    new DropTarget(this, this);
    this.setDragEnabled(true);
}

@Override
public void dragEnter(DropTargetDragEvent arg0) {
}

@Override
public void dragExit(DropTargetEvent arg0) {
}

@Override
public void dragOver(DropTargetDragEvent arg0) {
}

@Override
public void drop(DropTargetDropEvent dropTargetDropEvent) {
    System.out.println("Drop event");
    Transferable transferable = dropTargetDropEvent.getTransferable();
    for (DataFlavor d : transferable.getTransferDataFlavors()) {
        if (d.isRepresentationClassInputStream()) {
            dropTargetDropEvent
                    .acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            try {
                InputStream is = new BufferedInputStream(
                        (InputStream) transferable.getTransferData(d));

                ImageIcon image = new ImageIcon(ImageIO.read(is));
                this.insertIcon(image);
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } 

        } else {
            dropTargetDropEvent.rejectDrop();
        }
    }
    dropTargetDropEvent.getDropTargetContext().dropComplete(true);
}

@Override
public void dropActionChanged(DropTargetDragEvent arg0) {
}
}

用这个片段试试看

ImageIcon image = new ImageIcon(ImageIO.read(createImageInputStream(is)));

ImageIO.read()需要一个或,请注意,在处理图像时,大多数
NullPointerException
都是在图像、图像源或从该源读取时出现问题(更具体地说,读取图像返回null)时抛出的

用这个片段试试看

ImageIcon image = new ImageIcon(ImageIO.read(createImageInputStream(is)));

ImageIO.read()需要一个或,请注意,在处理图像时,大多数
NullPointerException
都是在图像、图像源或从该源读取时出现问题(更具体地说,读取图像返回null)时抛出的

你是指ImageIO.createImageInputStream(is)?你是指ImageIO.createImageInputStream(is)?