Comment afficher un fichier GIF animé dans une application Java?Afficher le fichier GIF animé
Répondre
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);
}
Lien très utile et intéressant, merci. – Benj
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);
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
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);
}
}
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);
}
Il est bon d'ajouter quelques explications sur votre code. En quoi est-ce différent, pourquoi ça marche? Merci (modérateur). –
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) {
}
//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);
}
}
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());
}
}
}
- 1. GIF animé dans ImageList/TabPage
- 2. précharger un gif animé
- 3. Gif animé dans android
- 4. GIF animé Android (Cupcake)
- 5. PHP crop animé gif
- 6. Le fichier gif animé ne fonctionne pas dans IE7
- 7. Le framerate animé * .gif est trop élevé
- 8. Réduire le GIF animé avec PHP GD
- 9. Comment afficher gif animé pendant une opération asynchrone longue?
- 10. afficher un gif animé pendant l'exécution d'un sous-marin
- 11. Silverlight 3 et gif animé
- 12. Gif animé ne pas animer sur soumettre
- 13. flex 3 gif animé pour le chargement du DataGrid
- 14. Comment utiliser un fichier .gif animé dans mon application iPhone?
- 15. AS3 déterminer differnce entre gif animé et non-animé?
- 16. Problème avec GIF animé dans WPF
- 17. Problème Python avec redimensionnement animé GIF
- 18. Problème avec gif animé sur Android
- 19. Gif animé ne s'affiche pas dans le contrôle webBrowser
- 20. Afficher la barre de chargement ou le fichier GIF animé pendant le chargement des fichiers CSS et JS?
- 21. Comment déterminer si un gif est animé?
- 22. Exporter de PowerPoint vers un gif animé?
- 23. Obtenir la durée d'un cadre GIF animé?
- 24. GIF animé ne fonctionnant pas dans FireFox après le cache
- 25. Favicon animé GIF pour Firefox et ICO statique pour repli
- 26. GIF animé dans la cellule SWT table/tree viewer
- 27. besoin de créer un GIF animé en C#
- 28. Internet Explorer n'affiche pas le fichier gif
- 29. Comment ajouter un gif animé à un bouton?
- 30. IE7 GIF animé apparaît comme une image statique?
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é –
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"); '. –
Je l'utilise avec Java8 et il n'anime pas mon GIF ... –