用URL对象封装这个地址,然后用openStream方法获得网络流读取图片数据。
接着你在用swing组件的的g2d对象把这个图画出来。
接着你在用swing组件的的g2d对象把这个图画出来。
解决方案 »
- jsp标签的区别?请教了
- 提问:逐个显示字体
- ORA-03115: unsupported network datatype or representation
- Java怎么导出word啊,试了好几种都不成
- 关于main method的问题
- int可否强制转换成boolean?
- 任何类都有构造函数......
- 用jcreater导入serverlet包后,使用serverlet包中的类不变颜色,应该怎么解决?
- 说到路径问题,我想问问
- jdbc 连接sybase11.9时出错,错误提示:JZ0D5: 装载协议 com.sybase.jdbc2.tds.tds 时出错
- 怎样得到某一天是星期几
- 关于读取文件 小困惑
使用自己的没问题。
但是使用楼主的URL就是不能读,很奇怪。
ImagePanel panel = new ImagePanel(new
ImageIcon("images/background.png").getImage()); JFrame frame = new JFrame("Hack #1: Create Image-Themed Components");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true); }
}
setSize(icon.getImage().getWidth(null),
icon.getImage().getHeight(null));
setIcon(icon);
setIconTextGap(0);
setBorder(null);
setText(null);
setOpaque(false);
} }
label.setLocation(29,37);
panel.add(label);
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;public class TestImagePanel {
public static void main(String[] args){
JFrame frame = new JFrame("Test Image Panel");
JLabel lbl = new JLabel();
BufferedImage image = null;
try {
URL imageURL = new URL("http://ptlogin2.qq.com/getimage");
InputStream is = imageURL.openConnection().getInputStream();
System.out.println();
image = ImageIO.read(is);
System.out.println("image is:"+image);
} catch (Exception e) {
e.printStackTrace();
}
Container c= frame.getContentPane();
c.setLayout(new FlowLayout());
c.add(lbl);lbl.setIcon(new ImageIcon(image));
frame.setBounds(200,100,300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
改成这样
Connection httpCon = imageURL.openConnection();
InputStream is = httpCon.getInputStream();
...
image = ImageIO.read(is);
httpCon.close();