import javax.swing.*;
import java.net.MalformedURLException;
import java.net.URL;public class Aloha extends JPanel {
private static void createAndShowGui() throws MalformedURLException {
ImageIcon icon = new ImageIcon(new URL("http://www.cppblog.com/images/cppblog_com/biao/Button.png"));
JLabel label = new JLabel(icon); JFrame frame = new JFrame();
frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGui();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}
解决方案 »
- 下面这个设计怎么实现。关于各个连锁店的互联设计
- 关于static 的两种应用难以区分
- 请教一个算法(脑袋有点晕了,望请高手帮忙)
- 请问如何选择不同程序入口来调试程序?
- 编的程序在Applet显示不出图片?
- 关于运行时确定数组长度的问题,请教各位大虾
- 在一个 JDesktopPane 中加入一个 JInternalFrame 如何让它初始化时最大化,并且选定,在最前面?
- question
- 关于JAVA和C/C++通过socket发送和接受数据的问题
- 为什么只要使用了Borland自己的类(DATABASE这些),APPLET就不能用了?
- java当中的object数组转换为string的问题
- javaSE swing生成图片中,clearRect()方法的使用疑惑
怎么看上去就那么别扭呢!
一,怎么解析显示图片,网页地址不是图片格式的,如ASP ASPX JSP等等。
二,怎么拉伸显示。你那个代码只是直接把图片扔在上面的。
三,因为是验证码图片。所以会有COOKIE的。请不要直接用URL来显示图片,要先下载BYTE。再显示。这样下载的时候我就可以自己存一份COOKIE了。再次谢谢了!
我怀疑有可能本来这些字节流就多出来了其它信息。或者解析的时候不正常。只得到字节流。
我也不知道这张图片具体格式是什么。这些原因也许有。
这网页的源码我提取出来了。
<img src="http://www.uxin.com/captcha/index.html">
怎么都是这个地址,无法显示到组件上。但网页上正常显示,不知为何?
执行下面的代码看看得到了什么。
HttpURLConnection connection = (HttpURLConnection) new URL("http://www.uxin.com/captcha/index.html?t=" + new Date().getTime()).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream response = connection.getInputStream();
byte[] buf = new byte[1024]; FileOutputStream out = new FileOutputStream("/Users/Biao/Desktop/x.html"); int len = 0;
while ((len = response.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
out.close();
你也可以看到的,为何在网页上打开就是图片了。我直接下载却成了网页,???
因为需要加HEAD才能正常得到见面数据。JAVA就是这么麻烦。
connection.setRequestProperty("Accept","text/html, application/xhtml+xml, */*");
这东西全部加上即可。结帖给分,再次谢谢。有时候讨论也是一种帮助。