2010-05-29 19 views

risposta

29

Utilizzando oscillazione si può semplicemente utilizzare 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

Per qualche ragione, se si ottiene il vostro 'oggetto ImageIcon' con qualcosa di simile questa icona' Icona = new ImageIcon (ImageIO.read (getClass() getResourceAsStream ("iconasresource.gif")).); 'La tua GIF non verrà animato –

+2

Infatti, la creazione di un ImageIcon con ImageIO.read non anima la gif per qualche motivo. Forse ovvio, ma puoi ottenere l'URL della risorsa con: 'URL url = getClass(). GetResource ("/img.gif ");'. –

+0

Lo sto usando con Java8 e non sta animando la mia GIF ... –

5

per il caricamento di Gif animate archiviati in un pacchetto sorgente (nel codice sorgente), questo ha funzionato per me:

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

Questo è esattamente ciò che non funziona per me. L'immagine viene caricata, ma viene mostrato solo il primo fotogramma, nessuna animazione. – 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

Questo lavoro per me!

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

È bene aggiungere alcune spiegazioni sul tuo codice. Com'è diverso, perché farlo funzionare. Grazie (moderatore). –

3

Sono venuto qui a cercare la stessa risposta, ma sulla base delle migliori risposte, mi si avvicinò con un codice più semplice. Spero che questo possa aiutare le ricerche future.

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

ho voluto mettere il file .gif in una GUI, ma visualizzati con altri elementi. E il file .gif verrebbe preso dal progetto java e non da un URL.

1 - superiore dell'interfaccia sarebbe un elenco di elementi in cui possiamo scegliere uno

2 - Centro sarebbe l'immagine GIF animata

3 - In basso visualizzerebbe l'elemento scelto dalla lista

Ecco il mio codice (ho bisogno di 2 file java, il primo (Interf.java) chiama il secondo (Display.java)):

1 - Interf.java

public class Interface_for { 

    public static void main(String[] args) { 

     Display Fr = new Display(); 

    } 
} 

2 - Display.java

Info: Be shure per creare una nuova cartella di origine (NUOVO> cartella di origine) nel progetto Java e mettere il .gif interno per poter essere visto come un file.

Ottengo il file gif con il codice qui sotto, quindi posso esportarlo in un progetto jar (è quindi animato).

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