2016-05-17 77 views
0

我想抓取来自在线的图像,然后用它在Jframe表单上显示。 “你好,世界!”将显示,但图像不会显示。任何想法家伙?干杯。Java图像抓取和显示

JFrame frame = new JFrame(); 
    frame.setSize(400, 400); 
    JLabel label = new JLabel("Hello, World!"); 
    String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l"; 
      try { 
       URL url = new URL(path); 
       ImageIcon image = new ImageIcon(url); 
       JLabel imageLabel = new JLabel(image); 
       label.setOpaque(true); 
       frame.add(imageLabel); 
       frame.add(label); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } catch (MalformedURLException ex) { 
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
      } 
+0

请在此处查看[问] - 如果有任何问题或需要告诉我们没有任何问题,请填写错误信息。 –

回答

0

看来你是代理服务器后面,你的代码是不是能够通过将实际值

System.setProperty("http.proxyHost", "<your proxy ip>"); 
    System.setProperty("http.proxyPort", "<proxy port>"); 
    System.setProperty("http.proxyUser", "<proxy user>"); 
    System.setProperty("http.proxyPassword", "<proxy password>"); 

或者,你可以连接到chart.finance.yahoo.com

请添加代理服务器设置程序提供这些作为命令行参数以及使用-D开关。详情请参阅:How to use proxy from java

1

JFrame使用BorderLayout通过默认,这意味着只有一个组件可以在(默认)位置CENTER显示。

基本上,发生了什么是label被取代了imageLabel,并已显示相反,尝试做这样的事情..

Example

JLabel imageLabel = new JLabel(image); 
label.setOpaque(true); 
frame.add(imageLabel); 
//frame.add(label); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

更多细节

我会看到How to use BorderLayout也使用ImageIO.readImageIcon来加载,至少会在图片无法加载时抛出IOException请参阅Reading/loading images以获取更多详细信息