Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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/6/entity-framework/4.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 是否有异步方式下载带有ImageIcon和URL的图像?_Java - Fatal编程技术网

Java 是否有异步方式下载带有ImageIcon和URL的图像?

Java 是否有异步方式下载带有ImageIcon和URL的图像?,java,Java,当我从URL获取图像时,我找不到在下载图像时显示加载图标的方法。以下是我的方法: private void fetchAdImage(String clickedLink) { try { // This gets completely ignored \/ adDisplayLabel.setIcon(new ImageIcon(MainFrame.class.getResource("/icons/spinner.png

当我从URL获取图像时,我找不到在下载图像时显示加载图标的方法。以下是我的方法:

    private void fetchAdImage(String clickedLink) {
        try {
            // This gets completely ignored \/
            adDisplayLabel.setIcon(new ImageIcon(MainFrame.class.getResource("/icons/spinner.png")));
            // /\
            URL link = new URL(clickedLink);
            ImageIcon adImage = new ImageIcon(link, "");
            int difference;
            if (adImage.getIconHeight() > adDisplayLabel.getHeight()) {
                difference = adImage.getIconHeight() + adDisplayLabel.getHeight();
            } else {
                difference = adImage.getIconHeight() - adDisplayLabel.getHeight();
            }
            adDisplayLabel.setIcon(drawBorder(Tools.getResizedIcon(adImage, adImage.getIconWidth() - difference, adDisplayLabel.getHeight())));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

其想法是在“onPreUpdate”阶段将JLabel的图标更改为“微调器”图像,并在“onSuccess”阶段更改为下载的图像。问题是:这种下载方法没有这种东西,前面的语句完全被忽略了。有没有一种异步方式可以下载Java中的图像?

多亏了@Slaw的建议(将代码封装在一个我不知道的类中),我成功地解决了这个问题。代码如下:

    private void fetchAdImage(String clickedLink) {
        try {
            SwingWorker<ImageIcon, Void> worker = new SwingWorker<ImageIcon, Void>() {
                ImageIcon adImage;
                int difference;

                @Override
                protected ImageIcon doInBackground() throws Exception {
                    adDisplayLabel.setIcon(new ImageIcon(MainFrame.class.getResource("/icons/spinner.png")));
                    URL link = new URL(clickedLink);
                    adImage = new ImageIcon(link, "");

                    if (adImage.getIconHeight() > adDisplayLabel.getHeight()) {
                        difference = adImage.getIconHeight() + adDisplayLabel.getHeight();
                    } else {
                        difference = adImage.getIconHeight() - adDisplayLabel.getHeight();
                    }
                    return adImage;
                }

                protected void done() {
                    adDisplayLabel.setIcon(drawBorder(Tools.getResizedIcon(adImage, adImage.getIconWidth() - difference, adDisplayLabel.getHeight())));
                }
            };
            worker.execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
private void fetchAdImage(字符串单击链接){
试一试{
SwingWorker worker=新SwingWorker(){
图像图标;
智力差异;
@凌驾
受保护的ImageIcon doInBackground()引发异常{
adDisplayLabel.setIcon(新的ImageIcon(MainFrame.class.getResource(“/icons/spinner.png”));
URL链接=新的URL(单击链接);
adImage=新图像图标(链接“”);
如果(adImage.getIconHeight()>adDisplayLabel.getHeight()){
差异=adImage.getIconHeight()+addisplaylab.getHeight();
}否则{
差异=adImage.getIconHeight()-adDisplayLabel.getHeight();
}
返回图像;
}
受保护的void done(){
adDisplayLabel.setIcon(drawBorder(Tools.getResizedIcon(adImage,adImage.getIconWidth()-difference,adDisplayLabel.getHeight()));
}
};
worker.execute();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

将抓取代码包装在一个?非常感谢@Slaw,我不知道这个类:o我已经设法做到了,我将发布答案。