如何在Java應用程序中顯示動畫GIF?顯示動畫GIF
顯示動畫GIF
回答
使用擺動,你可以簡單地使用一個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);
}
非常有用和有趣的鏈接,謝謝。 – Benj 2013-11-09 17:19:31
對於存儲在一個源代碼包(源代碼)加載GIF動畫,這個工作對我來說:
URL url = MyClass.class.getResource("/res/images/animated.gif");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel(imageIcon);
這正是不適合我的。圖像被加載,但只顯示第一幀,沒有動畫。 – lukfi 2015-12-27 09:54:07
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);
}
}
這個工作適合我!
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);
}
這是很好的添加一些關於你的代碼的解釋。它有什麼不同,爲什麼它工作。謝謝(主持人)。 – 2016-02-22 19:19:08
我來這裏尋找相同的答案,但根據最高的答案,我想出了一個更簡單的代碼。希望這將有助於未來的搜索。
Icon icon = new ImageIcon("src/path.gif");
try {
mainframe.setContentPane(new JLabel(icon));
} catch (Exception e) {
}
//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);
}
}
我希望把.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());
}
}
}
- 1. 顯示gif動畫
- 2. 計時動畫gif顯示
- 3. 顯示動畫GIF後顯示信息
- 4. python tkinter使用PIL顯示動畫GIF
- 5. IOS支持顯示GIF動畫圖像?
- 6. 在gridview中顯示GIF動畫
- 7. 在QLabel中顯示.gif動畫
- 8. 動畫gif不是動畫
- 9. XNA gif動畫
- 10. Android:動畫gif
- 11. DirectFB&GIF動畫
- 12. 動畫 - GIF
- 13. jQuery GIF動畫
- 14. Android動畫GIF
- 15. jQuery自動完成:如何顯示動畫GIF加載圖像
- 16. 動畫GIF在HTML5畫布
- 17. GIF動畫Qt中
- 18. 停止GIF動畫
- 19. 編輯動畫GIF
- 20. 創建GIF動畫
- 21. 在JAVAFX中處理時顯示gif動畫彈出框
- 22. 重型GC Android中顯示同時,.gif動畫
- 23. 未能在Android應用程序中顯示動畫GIF圖像
- 24. Qt - 在MainWindow上顯示由QListWidget選擇的動畫GIF
- 25. 從多圖像(動畫)GIF文件顯示單個圖像
- 26. PHP Gif GD庫動畫損失只顯示第一幀
- 27. Qt - 如何在QGraphicsPixmapItem中顯示gif(動畫)圖像
- 28. 在GTK中顯示動畫GIF#圖像構件
- 29. .gif中的動畫未在窗口中顯示形式
- 30. 動畫GIF不會顯示在瀏覽器
出於某種原因,如果你有這樣的事情'圖標圖標=新的ImageIcon讓你'ImageIcon'對象(ImageIO.read(的getClass()的getResourceAsStream( 「iconasresource.gif」)));你' GIF不會得到動畫 – 2014-02-22 08:11:36
事實上,使用ImageIO.read創建ImageIcon不會因爲某種原因爲GIF製作動畫。也許很明顯,但您可以通過以下方式獲取資源的URL:URL URL = getClass()。getResource(「/ img.gif」);'。 – 2016-03-11 14:54:27
我用它與Java8,它不是動畫我的GIF ... – 2017-02-22 14:01:19