Java 显示动画 GIF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2935232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:37:14  来源:igfitidea点击:

Show animated GIF

javaimagegifanimated-gif

提问by SamSol

How do you display an animated GIF in a Java application?

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

回答by stacker

Using swing you could simply use a JLabel

使用 Swing 你可以简单地使用 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);
    }

回答by Scott Wardlaw

For loading animated gifs stored in a source package (in the source code), this worked for me:

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

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

回答by jamil

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);
}
}

回答by Alan Moratorio

This work for me!

这对我有用!

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);
    }

回答by Gray

I came here searching for the same answer, but based on the top answers, I came up with an easier code. Hope this will help future searches.

我来到这里寻找相同的答案,但根据最佳答案,我想出了一个更简单的代码。希望这将有助于未来的搜索。

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

回答by Bredosen

//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);
   }
}

回答by Andy McRae

I wanted to put the .gif file in a GUI but displayed with other elements. And the .gif file would be taken from the java project and not from an URL.

我想将 .gif 文件放在 GUI 中,但与其他元素一起显示。.gif 文件将从 java 项目中获取,而不是从 URL 中获取。

1 - Top of the interface would be a list of elements where we can choose one

1 - 界面顶部将是一个元素列表,我们可以从中选择一个

2 - Center would be the animated GIF

2 - 中心将是动画 GIF

3 - Bottom would display the element chosen from the list

3 - 底部将显示从列表中选择的元素

Here is my code (I need 2 java files, the first (Interf.java) calls the second (Display.java)):

这是我的代码(我需要 2 个 java 文件,第一个(Interf.java)调用第二个(Display.java)):

1 - Interf.java

1 - Interf.java

public class Interface_for {

    public static void main(String[] args) {

        Display Fr = new Display();

    }
}

2 - Display.java

2 - Display.java

INFOS: Be shure to create a new source folder (NEW > source folder) in your java project and put the .gif inside for it to be seen as a file.

信息:请务必在您的 java 项目中创建一个新的源文件夹(NEW > 源文件夹)并将 .gif 放入其中以便将其视为文件。

I get the gif file with the code below, so I can it export it in a jar project(it's then animated).

我得到了带有下面代码的 gif 文件,所以我可以将它导出到一个 jar 项目中(然后它被动画化)。

URL url = getClass().getClassLoader().getResource("fire.gif");

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());
      }
  }
}

回答by Mehdi

Try this:

尝试这个:

// 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);

Found on this tutorial on how to display animated gif in java

在有关如何在 Java 中显示动画 gif 的本教程中找到

Or live on youtube : https://youtu.be/_NEnhm9mgdE

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