2010-05-29 8 views

Répondre

29

utilisant Swing vous pouvez simplement utiliser un 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); 
    } 
+6

Pour une raison quelconque, si vous obtenez votre 'objet ImageIcon' avec quelque chose comme ceci' icône Icône = new ImageIcon (ImageIO.read (getClass() getResourceAsStream ("iconasresource.gif")).); 'Votre GIF ne sera pas animé –

+2

En effet, créer un ImageIcon avec ImageIO.read n'anime pas le gif pour une raison quelconque. Peut-être évident, mais vous pouvez obtenir l'URL de la ressource avec: URL url = getClass(). GetResource ("/ img.gif"); '. –

+0

Je l'utilise avec Java8 et il n'anime pas mon GIF ... –

5

Pour gifs animés de chargement stockées dans un paquet source (dans le code source), cela a fonctionné pour moi:

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

C'est exactement ce qui ne fonctionne pas pour moi. L'image est chargée, mais seule la première image est affichée, pas d'animation. – lukfi

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

Ce travail pour moi!

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

Il est bon d'ajouter quelques explications sur votre code. En quoi est-ce différent, pourquoi ça marche? Merci (modérateur). –

3

Je suis venu ici à la recherche de la même réponse, mais sur la base des réponses les plus élevées, j'ai trouvé un code plus facile. J'espère que cela aidera les recherches futures.

Icon icon = new ImageIcon("src/path.gif"); 
      try { 
       mainframe.setContentPane(new JLabel(icon)); 
      } catch (Exception e) { 
      } 
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); 
    } 
} 
0

Je voulais mettre le fichier .gif dans une interface graphique, mais affiche avec d'autres éléments. Et le fichier .gif serait extrait du projet java et non d'une URL.

1 - Haut de l'interface serait une liste d'éléments où l'on peut choisir un

2 - Centre serait le GIF animé

3 - Bas afficherait l'élément choisi dans la liste

Voici mon code (j'ai besoin 2 fichiers java, le premier (Interf.java) appelle la seconde (Display.java)):

1 - Interf.java

public class Interface_for { 

    public static void main(String[] args) { 

     Display Fr = new Display(); 

    } 
} 

2 - Display.java

INFOS: Soyez shure pour créer un nouveau dossier source (NOUVEAU> dossier source) dans votre projet java et mettre le .gif à l'intérieur pour qu'il soit considéré comme un fichier.

Je reçois le fichier gif avec le code ci-dessous, donc je peux l'exporter dans un projet jar (il est ensuite animé).

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