有什么问题,我无法通过
保存在硬盘驱动器上而无法下载图像try {
sentenceLabel= new JLabel(new ImageIcon(ImageIO.read(new URL("http://www.google.ru/intl/en_com/images/logo_plain.png"))));
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (IOException ex) {
// "http://img.yandex.net/i/wiz"+imgType.trim()+".png"
ex.printStackTrace();
}
怎么了?抱歉没有问题
答案 0 :(得分:1)
您可以使用此代码下载图片:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class ImageDownloader
{
public static void main(String[] args )
{
BufferedImage image =null;
try{
URL url =new URL("http://developerfeed.com/sites/default/files
/have_a_question.png");
// read the url
image = ImageIO.read(url);
for png
ImageIO.write(image, "png",new File("/tmp/have_a_question.png"));
// for jpg
ImageIO.write(image, "jpg",new File("/tmp/have_a_question.jpg"));
}catch(IOException e){
e.printStackTrace();
}
}}
答案 1 :(得分:1)
您的代码完全正常。这会向我展示内容窗格中的图片。
JFrame frame = new JFrame();
frame.setTitle("Polygons");
frame.setSize(550, 550);
Container contentPane = frame.getContentPane();
try {
JLabel sentenceLabel= new JLabel(new ImageIcon(
ImageIO.read(new URL(
"http://www.google.ru/intl/en_com/images/logo_plain.png"))));
contentPane.add(sentenceLabel);
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
} catch (IOException ex) {
// "http://img.yandex.net/i/wiz"+imgType.trim()+".png"
ex.printStackTrace();
}
frame.show();