下载和显示图像时出现问题

时间:2019-02-13 17:36:05

标签: java swing

我用Java编写了一个程序,只需按gui中的一个按钮,就可以从互联网上下载一个链接的图像,并将其保存到计算机的特定文件夹中。我接下来要做的是在屏幕上显示该图像。请注意,URL输入不一定是图像本身的URL,而是包含图像的HTML网页的URL。问题是,我不能简单地抢先创建ImageIcon对象,因为在编译时系统中还没有该图像文件。

面板类

public class AdditionPanel extends JPanel
{

    // ...
    static JTextPane textpane;
    JLabel paneInstructions;
    JButton linkOk;

    public AdditionPanel() throws IOException
    {

        textpane = new JTextPane();         
        paneInstructions = new JLabel("Paste the link here:");
        linkOk = new JButton(" OK ");

        // ...

        linkOk.addActionListener(new LinkOkPressed());

        // ...

        this.add(textpane);
        this.add(paneInstructions);
        this.add(linkOk);
    }
}

我曾经有过一个想法,就是为该按钮创建一个ActionListener,然后仅在按下按钮并因此下载了文件之后才尝试访问该文件。在这种情况下,我不知道如何在另一个类的JPanel上显示图像。

动作监听器

public class LinkOkPressed implements ActionListener
{
    JLabel test;
    @Override
    public void actionPerformed(ActionEvent e)
    {
        // ImageDownloader is a class I have created, that simply saves the image
        // from the given URL in a predetermined directory
        ImageDownloader.saveImage(ImageDownloader.getImageUrl(AdditionPanel.textpane.getText()));
        ImageIcon poster = new ImageIcon(getClass().getResource("/resources/myimage.png"));
        test= new JLabel(poster);
        AdditionPanel.add(test); // Does not work
    }

}

那么,我可以从其他类向面板添加图像吗,还是有更好的方法来访问在应用程序运行时下载的文件?感谢您的帮助。

ImageDownloader

public class ImageDownloader
{
    public static String getImageUrl(String imdbLink)
    {
        String imageLink = "";
        try
        {
            Document doc = Jsoup.connect(imdbLink).get();
            Elements divs = doc.getElementsByClass("poster");
            Element poster = divs.first();
            Elements image = poster.getElementsByTag("a");
            Element downloadImage = image.first();
            Elements img = downloadImage.getElementsByTag("img");
            imageLink = img.attr("src");
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return imageLink;
    }


    public static void saveImage(String imageLink)
    {
        BufferedImage image = null;
        try
        {
            URL url =new URL(imageLink);
            image = ImageIO.read(url);
            ImageIO.write(image, "png", new File("C:\\...\\resources\\myimage.png"));
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

详细介绍 PM 77-1 的评论。

public class LinkOkPressed implements ActionListener
{
    JLabel test;
    @Override
    public void actionPerformed(ActionEvent e)
    {
        // ImageDownloader is a class I have created, that simply saves the image
        // from the given URL in a predetermined directory
        ImageDownloader.saveImage(ImageDownloader.getImageUrl(AdditionPanel.textpane.getText()));
        ImageIcon poster = new ImageIcon(getClass().getResource("/resources/myimage.png"));
        test= new JLabel(poster);
        Object obj = e.getSource(); // "obj" is really "linkOk" from class "AdditionPanel"
        java.awt.Container parent = ((JButton) obj).getParent(); // "parent" is instance of "AdditionPanel"
        ((JPanel) parent).add(test);
    }
}

答案 1 :(得分:1)

SwingWorker示例。

像这样创建一个单独的类...

public class SaveImage extends javax.swing.SwingWorker<Void, Void> {
    private AdditionPanel additionPanel;

    public SaveImage(AdditionPanel addPanel) {
        additionPanel = addPanel;
    }

    @Override
    protected Void doInBackground() throws Exception {
        ImageDownloader.saveImage(ImageDownloader.getImageUrl(AdditionPanel.textpane.getText()));
        // Wait until the image is saved.
        // Since I don't have code for class "ImageDownloader", can't tell you how to do this.
        return null;
    }

    @Override
    protected void done() {
        javax.swing.ImageIcon poster = new javax.swing.ImageIcon(getClass().getResource("/resources/myimage.png"));
        javax.swing.JLabel test= new javax.swing.JLabel(poster);
        additionPanel.add(test);
    }
}

然后像这样更改您的LinkOkPressed类...

public class LinkOkPressed implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object obj = e.getSource(); // "obj" is really "linkOk" from class "AdditionPanel"
        java.awt.Container parent = ((JButton) obj).getParent(); // "parent" is instance of "AdditionPanel"
        SaveImage saveImage = new SaveImage((AdditionPanel) parent);
        saveImage.execute();
    }
}