2010-05-29 143 views

回答

29

使用擺動,你可以簡單地使用一個JLabel

public static void main(String[] args) throws MalformedURLException { 

     URL url = new URL("<URL to your Animated GIF>"); 
     Icon icon = new ImageIcon(url); 
     JLabel label = new JLabel(icon); 

     JFrame f = new JFrame("Animation"); 
     f.getContentPane().add(label); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 
+6

出於某種原因,如果你有這樣的事情'圖標圖標=新的ImageIcon讓你'ImageIcon'對象(ImageIO.read(的getClass()的getResourceAsStream( 「iconasresource.gif」)));你' GIF不會得到動畫 – 2014-02-22 08:11:36

+2

事實上,使用ImageIO.read創建ImageIcon不會因爲某種原因爲GIF製作動畫。也許很明顯,但您可以通過以下方式獲取資源的URL:URL URL = getClass()。getResource(「/ img.gif」);'。 – 2016-03-11 14:54:27

+0

我用它與Java8,它不是動畫我的GIF ... – 2017-02-22 14:01:19

5

對於存儲在一個源代碼包(源代碼)加載GIF動畫,這個工作對我來說:

URL url = MyClass.class.getResource("/res/images/animated.gif"); 
ImageIcon imageIcon = new ImageIcon(url); 
JLabel label = new JLabel(imageIcon); 
+0

這正是不適合我的。圖像被加載,但只顯示第一幀,沒有動畫。 – lukfi 2015-12-27 09:54:07

-2
public class aiubMain { 
public static void main(String args[]) throws MalformedURLException{ 
    //home frame = new home(); 
    java.net.URL imgUrl2 = home.class.getResource("Campus.gif"); 

Icon icon = new ImageIcon(imgUrl2); 
JLabel label = new JLabel(icon); 

JFrame f = new JFrame("Animation"); 
f.getContentPane().add(label); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.pack(); 
f.setLocationRelativeTo(null); 
f.setVisible(true); 
} 
} 
4

這個工作適合我!

public void showLoader(){ 
     URL url = this.getClass().getResource("images/ajax-loader.gif"); 
     Icon icon = new ImageIcon(url); 
     JLabel label = new JLabel(icon); 
     frameLoader.setUndecorated(true); 
     frameLoader.getContentPane().add(label); 
     frameLoader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frameLoader.pack(); 
     frameLoader.setLocationRelativeTo(null); 
     frameLoader.setVisible(true); 
    } 
+0

這是很好的添加一些關於你的代碼的解釋。它有什麼不同,爲什麼它工作。謝謝(主持人)。 – 2016-02-22 19:19:08

3

我來這裏尋找相同的答案,但根據最高的答案,我想出了一個更簡單的代碼。希望這將有助於未來的搜索。

Icon icon = new ImageIcon("src/path.gif"); 
      try { 
       mainframe.setContentPane(new JLabel(icon)); 
      } catch (Exception e) { 
      } 
0
//Class Name 
public class ClassName { 
//Make it runnable 
public static void main(String args[]) throws MalformedURLException{ 
//Get the URL 
URL img = this.getClass().getResource("src/Name.gif"); 
//Make it to a Icon 
Icon icon = new ImageIcon(img); 
//Make a new JLabel that shows "icon" 
JLabel Gif = new JLabel(icon); 

//Make a new Window 
JFrame main = new JFrame("gif"); 
//adds the JLabel to the Window 
main.getContentPane().add(Gif); 
//Shows where and how big the Window is 
main.setBounds(x, y, H, W); 
//set the Default Close Operation to Exit everything on Close 
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//Open the Window 
main.setVisible(true); 
    } 
} 
0

我希望把.gif文件的GUI,但與其他元素顯示。 .gif文件將從java項目中獲取,而不是從URL獲取。

1 - 頂端的接口將是元件的列表,其中我們可以選擇一個

2 - 中心將是動畫GIF

3 - 底部將顯示從列表中選擇的元素

這裏是我的代碼(我需要2個java文件,第(Interf.java)調用第二(Display.java)):

1 - Interf.java

public class Interface_for { 

    public static void main(String[] args) { 

     Display Fr = new Display(); 

    } 
} 

2 - Display.java

INFOS:是舒爾建立在你的Java項目中的新源文件夾(NEW>源文件夾),然後將.gif文件內爲它被視爲一個文件。

我得到了下面的代碼的gif文件,所以我可以把它導出到一個jar項目中(然後動畫)。

URL url = getClass()。getClassLoader()。getResource(「fire.gif」);

public class Display extends JFrame { 
    private JPanel container = new JPanel(); 
    private JComboBox combo = new JComboBox(); 
    private JLabel label = new JLabel("A list"); 
    private JLabel label_2 = new JLabel ("Selection"); 

    public Display(){ 
    this.setTitle("Animation"); 
    this.setSize(400, 350); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    container.setLayout(new BorderLayout()); 
    combo.setPreferredSize(new Dimension(190, 20)); 
    //We create te list of elements for the top of the GUI 
    String[] tab = {"Option 1","Option 2","Option 3","Option 4","Option 5"}; 
    combo = new JComboBox(tab); 

    //Listener for the selected option 
    combo.addActionListener(new ItemAction()); 

    //We add elements from the top of the interface 
    JPanel top = new JPanel(); 
    top.add(label); 
    top.add(combo); 
    container.add(top, BorderLayout.NORTH); 

    //We add elements from the center of the interface 
    URL url = getClass().getClassLoader().getResource("fire.gif"); 
    Icon icon = new ImageIcon(url); 
    JLabel center = new JLabel(icon); 
    container.add(center, BorderLayout.CENTER); 

    //We add elements from the bottom of the interface 
    JPanel down = new JPanel(); 
    down.add(label_2); 
    container.add(down,BorderLayout.SOUTH); 

    this.setContentPane(container); 
    this.setVisible(true); 
    this.setResizable(false); 
    } 
    class ItemAction implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      label_2.setText("Chosen option: "+combo.getSelectedItem().toString()); 
     } 
    } 
}