Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.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 SWT:复合材料内部的图像;获取IllegalArgumentException_Java_Swt - Fatal编程技术网

Java SWT:复合材料内部的图像;获取IllegalArgumentException

Java SWT:复合材料内部的图像;获取IllegalArgumentException,java,swt,Java,Swt,我目前正在使用SWT和eclipse的WindowBuilder制作一个独立的应用程序(不是eclipse插件),我正在测试将一个图像放入一个组合中,以将其用作一种将图像轻松放入组合中的方法,当我尝试绘制图像时,它抛出一个IllegalArgumentException。我不知道发生了什么,我正在寻找解释/替代方案。发生了什么,我将如何解决这个问题 如果我注释掉e.gc.drawImage行,就不会抛出异常 下面是给出错误的代码: import org.eclipse.swt.SWT; impo

我目前正在使用SWT和eclipse的WindowBuilder制作一个独立的应用程序(不是eclipse插件),我正在测试将一个图像放入一个组合中,以将其用作一种将图像轻松放入组合中的方法,当我尝试绘制图像时,它抛出一个IllegalArgumentException。我不知道发生了什么,我正在寻找解释/替代方案。发生了什么,我将如何解决这个问题

如果我注释掉
e.gc.drawImage
行,就不会抛出异常

下面是给出错误的代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GUI {
    public static final Display display = Display.getDefault();;
    private final Shell shell;

    public GUI() {
        shell = new Shell(display);
        shell.setLayout(new FillLayout(SWT.HORIZONTAL));
    }

    public static void main(String[] args) {
        GUI window = new GUI();
        window.open();
    }

    public void open() {
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    private void createContents() {
        shell.setSize(450, 300);

        ImageTest img = new ImageTest(shell, SWT.NONE);
    }
}

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Canvas;

public class ImageTest extends Composite {
    public ImageTest(Composite parent, int style) {
        super(parent, style);
        setLayout(new FillLayout(SWT.HORIZONTAL));

        final Image img = new Image(GUI.display, "img.gif");

            // I tried drawing the image to both a canvas and the composite its self. Same outcome.
        Canvas canvas = new Canvas(this, SWT.NONE);
        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(img, 0, 0); // If I comment this out, it runs fine.
            }
        });

        img.dispose();
    }

    @Override
    protected void checkSubclass() {}
}
如果您有任何帮助,我们将不胜感激。

API文档说,在以下情况下,将抛出IllegalArgumentException。可能是图像对象为空

IllegalArgumentException -
ERROR_NULL_ARGUMENT - if the image is null
ERROR_INVALID_ARGUMENT - if the image has been disposed
ERROR_INVALID_ARGUMENT - if the given coordinates are outside the bounds of the image
API文档指出,在以下情况下,将抛出IllegalArgumentException。可能是图像对象为空

IllegalArgumentException -
ERROR_NULL_ARGUMENT - if the image is null
ERROR_INVALID_ARGUMENT - if the image has been disposed
ERROR_INVALID_ARGUMENT - if the given coordinates are outside the bounds of the image

出现错误的原因是,当paintControl尝试绘制图像时,图像已被释放。您可以在
ImageTest
构造函数结束时自己处理它,然后才有机会调用paint listener

通过将
img
作为类的成员变量,然后重写dispose方法来执行清理,可以避免这种情况:

@Override
public void dispose() {
    this.img.dispose();
    super.dispose();
}
别忘了拆下电线

img.dispose();

从构造器中。

您会收到错误,因为当paintControl尝试绘制图像时,图像已被释放。您可以在
ImageTest
构造函数结束时自己处理它,然后才有机会调用paint listener

通过将
img
作为类的成员变量,然后重写dispose方法来执行清理,可以避免这种情况:

@Override
public void dispose() {
    this.img.dispose();
    super.dispose();
}
别忘了拆下电线

img.dispose();
从你的构造器