Wie zeigen Sie ein animiertes GIF in einer Java-Anwendung an?
Mit Swing können Sie einfach ein JLabel verwenden
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);
}
Für das Laden von animierten GIFs, die in einem Quellpaket gespeichert sind (im Quellcode), hat dies für mich funktioniert:
URL url = MyClass.class.getResource("/res/images/animated.gif");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel(imageIcon);
Diese Arbeit für mich!
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);
}
Ich bin hierher auf der Suche nach der gleichen Antwort gekommen, aber basierend auf den ersten Antworten habe ich einen einfacheren Code gefunden. Hoffen Sie, dass dies zukünftigen Suchanfragen helfen wird.
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);
}
}
Versuche dies:
// We 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);
In diesem Tutorial zu animierte GIFs in Java anzeigen
Ich wollte die GIF-Datei in eine grafische Benutzeroberfläche einfügen, die jedoch mit anderen Elementen angezeigt wurde. Und die GIF-Datei würde aus dem Java-Projekt und nicht aus einer URL stammen.
1 - Oben auf der Oberfläche befindet sich eine Liste von Elementen, aus denen wir eines auswählen können
2 - Zentrum wäre das animierte GIF
3 - Unten würde das aus der Liste ausgewählte Element anzeigen
Hier ist mein Code (ich brauche 2 Java-Dateien, die erste (Interf.Java) ruft die zweite (Display.Java) auf):
1 - Interf.Java
public class Interface_for {
public static void main(String[] args) {
Display Fr = new Display();
}
}
2 - Display.Java
INFOS: Stellen Sie sicher, dass Sie einen neuen Quellordner (NEU> Quellordner) in Ihrem Java-Projekt erstellen und die GIF-Datei darin ablegen, um sie als Datei anzuzeigen.
Ich bekomme die GIF-Datei mit dem Code unten, damit ich sie in ein Jar-Projekt exportieren kann (es wird dann animiert).
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());
}
}
}