我正在尝试使用从 URL 获取的图像来设置 JButton ImageIcon。这是我的代码:
try{
URL imageFile=new URL(picturePath);
carPic=ImageIO.read(imageFile);
picturePane.setIcon(new ImageIcon(carPic));//this line gives the exception
}catch(IOException e){
e.printStackTrace();
}
其中“picturePath”是图像 URL。
我打算使用的URL只是通过“复制图像地址”获得的普通URL,内容如下:“https://www.topgear.com/sites/default/files/544822_0.jpg”
我是否必须对 URL 进行一些操作才能使其正常工作,或者代码本身是否存在其他问题?
最佳答案
我已经找到了解决您遇到的问题的方法,我认为它可能会有所帮助:) Getting image from url
关于java - 从 URL 获取图像返回 NullPointerException (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60554266/