如何在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