gpt4 book ai didi

java - 下载和显示图像的问题

转载 作者:行者123 更新时间:2023-12-01 17:46:24 25 4
gpt4 key购买 nike

我用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 上显示来自不同类的图像。

Action 监听器

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
}

}

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

图像下载器

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();
}
}
}

最佳答案

详细阐述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);
}
}

关于java - 下载和显示图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54676260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com