显示动画GIF

时间:2010-05-29 13:29:11

标签: java image gif animated-gif

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

10 个答案:

答案 0 :(得分:30)

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

答案 1 :(得分:5)

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

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

答案 2 :(得分:4)

答案 3 :(得分:4)

这项工作适合我!

unique(combn(strsplit(x, "")[[1]],3, FUN=paste, collapse=""))

答案 4 :(得分:3)

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

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

答案 5 :(得分:0)

<i class="icon-add-circle" tabindex="0" >
             ::before 
</i>

答案 6 :(得分:0)

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

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

2 - 中心将是动画GIF

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

这是我的代码(我需要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&gt;源文件夹),并将.gif放在里面,以便将其视为文件。

我使用下面的代码获取gif文件,因此我可以将其导出到jar项目中(然后将其设置为动画)。

URL url = getClass()。getClassLoader()。getResource(&#34; fire.gif&#34;);

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

答案 7 :(得分:0)

尝试一下:

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

答案 8 :(得分: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);
}

使用“文件:”作为 URL

答案 9 :(得分:-2)

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