展示动态GIF图

37

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

11个回答

31

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

13
如果你像这样获得ImageIcon对象 Icon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("iconasresource.gif")));,由于某些原因,你的 GIF 不会动画显示。 - Jaime Hablutzel
9
使用 ImageIO.read 创建 ImageIcon 并不能使 GIF 动画播放,原因不太清楚。也许很明显,但你可以通过以下方式获取资源的 URL: URL url = getClass().getResource("/img.gif"); - José Ramón
3
这是一个可怕的API... 我一直在尝试弄清楚为什么GIF图没有动画效果。在我看到有关 "ImageIO" 的评论之前,无法在网上找到任何信息。难以置信。 - Dioxin
@stacker,你如何改变gif的大小和位置?我正在制作一个视频游戏,我希望gif能跟随玩家移动。 - Laser Infinite

5

对于在源代码中存储的动画gif加载,我使用以下方法:

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

2
这正是对我无效的。图像已加载,但只显示第一帧,没有动画。 - lukfi


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

最好在你的代码中添加一些解释。它有什么不同之处,为什么它能够工作。谢谢(管理员)。 - K.Nicholas

3

我来这里寻找同样的答案,但是根据顶部答案,我想出了一段更简单的代码。希望这能帮助未来的搜索。

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

1

快速简便的代码:

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

label.setIcon(icon);

别忘了让JFrame可见并调整大小。


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

提示:一定要在您的 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());
      }
  }
}

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

使用 "file:" 作为 URL


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

-1

试试这个:

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

如何在Java中显示动态GIF的教程中发现

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接