在Java中异步加载基于网络的映像

在Java中异步加载基于网络的映像,java,image,swing,asynchronous,loading,Java,Image,Swing,Asynchronous,Loading,我必须能够加载和绘制X数量的图像位于一个基于网络的驱动器。 我需要帮助找到一种异步加载图像的方法 java.net.URL Loc = new URL("http://auroragm.sourceforge.net/GameCover/GameCases/Mass-Effect.png"); JLabel lbl = new JLabel(); lbl.setIcon((anotherIcon = new ImageIcon(Loc))); 上面是一个加载到GUI线程上的图

我必须能够加载和绘制X数量的图像位于一个基于网络的驱动器。 我需要帮助找到一种异步加载图像的方法

 java.net.URL Loc = new URL("http://auroragm.sourceforge.net/GameCover/GameCases/Mass-Effect.png");
    JLabel lbl = new JLabel();
    lbl.setIcon((anotherIcon = new ImageIcon(Loc)));

上面是一个加载到GUI线程上的图像,因此如果再加载20个,它将冻结。任何帮助都将不胜感激

简短回答:您应该将图像加载到另一个线程上

Swing确实为此提供了一组很好的类和模式:


在单独的线程中加载图像。请将以下代码视为伪代码:

final java.net.URL Loc = new URL("http://.../Mass-Effect.png");
Thread t = new Thread(new Runnable() {
    public void run() {
        Object content = Loc.getContent();
        // content would be probably some Image class or byte[]

        // or:
        // InputStream in = Loc.openStream();
        // read image from in
    }
);

线程可能很有用,您可以限制同时运行的线程数。