2014-03-06 96 views
-2

到的JPanel我想要加載動畫GIF到JPanel的,所以我看怎麼做了,但是當我運行程序如何加載動畫GIF從MAC

我得到這個消息:

java.net.MalformedURLException: no protocol: /Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif 

我檢查我的gif路徑和它的正確!

那麼我該怎麼做?

我的代碼:

URL url; 
      try { 
       url = new URL("/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif"); 
       Icon imgGif = new ImageIcon(url); 
       JLabel lblGif = new JLabel(); 
       lblGif.setIcon(imgGif); 
       lblGif.setBounds(100, 100, 400, 450); 
       switchPanel.add(lblGif); 
      } catch (MalformedURLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

我解決它感謝所有反正:)

+0

你可以嘗試添加file://到URI嗎? –

+0

「URL」的Javadocs的哪一部分讓你感到困惑?這不是一個URL,因爲它沒有協議。 –

+0

yes..stil no help @ mig-25foxbat –

回答

0

與解決它:

String path = "/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif"; 

       Icon imgGif = new ImageIcon(path); 
       JLabel lblGif = new JLabel(); 
       lblGif.setIcon(imgGif); 
       lblGif.setBounds(150,150, 455, 170); 
       switchPanel.add(lblGif); 
+1

在部署時,這些資源可能會變成[tag:embedded-resource]。在這種情況下,資源必須通過'URL'而不是'File'訪問。查看標籤的[info page](http://stackoverflow.com/tags/embedded-resource/info),以獲得一個「URL」。 –

0
 Try This 
     import java.awt.*; 
     import javax.swing.JFrame; 
     import javax.swing.JPanel; 
     import java.awt.BorderLayout; 
     import javax.swing.JLabel; 
     import javax.swing.ImageIcon; 

class MainFrame extends JFrame { 
    JPanel contentPane; 
    JLabel imageLabel = new JLabel(); 
    JLabel headerLabel = new JLabel(); 

public MainFrame() { 
    try { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     contentPane = (JPanel) getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 
     setSize(new Dimension(400, 300)); 
     headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16)); 
     headerLabel.setText("Animated Image in java"); 
     contentPane.add(headerLabel, java.awt.BorderLayout.NORTH); 
     ImageIcon ii = new ImageIcon(this.getClass().getResource(
       "activity.gif")); 
     imageLabel.setIcon(ii); 
     contentPane.add(imageLabel, java.awt.BorderLayout.CENTER); 
     // show it 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
    } 
    catch (Exception exception) { 
     exception.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    new MainFrame(); 
} 

} 

Here you can see gif image inside frame

在這裏你可以看到gif圖像內部框架