【问题标题】:Show animated GIF显示动画 GIF
【发布时间】:2011-02-25 11:41:31
【问题描述】:

如何在 Java 应用程序中显示动画 GIF?

【问题讨论】:

    标签: java image gif animated-gif


    【解决方案1】:

    使用 Swing 您可以简单地使用 JLabel:

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("<url_to_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);
    }
    

    【讨论】:

    • 由于某种原因,如果你的 ImageIcon 对象带有类似 Icon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("iconasresource.gif"))); 这样的东西,你的 GIF 将不会被动画化
    • 确实,由于某种原因,使用 ImageIO.read 创建 ImageIcon 不会为 gif 设置动画。也许很明显,但您可以通过以下方式获取资源的 URL:URL url = getClass().getResource("/img.gif");
    • 这是一个可怕的 API... 试图弄清楚为什么 GIF 没有动画。在我看到关于 ImageIO 的评论之前,我无法在网上找到任何东西。难以置信。
    • @stacker 你如何改变 gif 的大小和位置?我正在创建一个视频游戏,我希望 gif 跟随玩家。
    【解决方案2】:
    【解决方案3】:

    对于加载存储在源包中(在源代码中)的动画 gif,这对我有用:

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

    【讨论】:

    • 这正是对我不起作用的。图片已加载,但只显示第一帧,没有动画。
    【解决方案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);
        }
    

    【讨论】:

    • 最好添加一些关于您的代码的解释。它有什么不同,为什么它起作用。谢谢(版主)。
    【解决方案5】:

    我来这里是为了寻找相同的答案,但根据最热门的答案,我想出了一个更简单的代码。希望这对以后的搜索有所帮助。

    Icon icon = new ImageIcon("src/path.gif");
                try {
                    mainframe.setContentPane(new JLabel(icon));
                } catch (Exception e) {
                }
    

    【讨论】:

      【解决方案6】:

      快速简单的代码:

      Icon icon = new ImageIcon("src/image.gif");
      JLabel label = new JLabel();
      
      label.setIcon(icon);
      

      不要忘记让 JFrame 可见并调整大小。

      【讨论】:

        【解决方案7】:
        //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);
           }
        }
        

        【讨论】:

          【解决方案8】:

          我想将 .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 - 显示.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());
                }
            }
          }
          

          【讨论】:

            【解决方案9】:
            JLabel mainLabel = new JLabel();
            FileChooser chooser = new FileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setMultiSelectionEnabled(false);
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Image", "png", "jpg", "gif");
            chooser.setFileFilter(filter);
            chooser.setDialogTitle(Lang.T("Open Image") + "...");
            int returnVal = chooser.showOpenDialog(getParent());
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                URL url;
                try {
                    url = new URL("file:" + chooser.getSelectedFile().getPath());
                } catch (Exception e) {
                    url = null;
                }
            
                Icon icon = new ImageIcon(url);
                mainLabel.setIcon(icon);
            }
            

            使用“文件:”作为 URL

            【讨论】:

              【解决方案10】:

              试试这个:

              // I suppose you have already set your JFrame 
              Icon imgIcon = new ImageIcon(this.getClass().getResource("ajax-loader.gif"));
              JLabel label = new JLabel(imgIcon);
              label.setBounds(668, 43, 46, 14); // for example, you can use your own values
              frame.getContentPane().add(label);
              

              how to display animated gif in java上的本教程中找到

              或在 youtube 上直播:https://youtu.be/_NEnhm9mgdE

              【讨论】:

                【解决方案11】:
                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);
                    }
                }
                

                【讨论】:

                  猜你喜欢
                  • 2016-08-23
                  • 2012-09-15
                  • 1970-01-01
                  • 2013-02-10
                  • 2012-09-15
                  • 2013-03-14
                  • 2014-06-10
                  相关资源
                  最近更新 更多