2016-05-17 70 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以獲取更多詳細信息