2010-05-29 33 views

Respuesta

29

Usando oscilación simplemente podría utilizar 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

Por alguna razón, si usted consigue su objeto 'ImageIcon' con algo como esto' icono Icono = new ImageIcon (ImageIO.read (getClass() getResourceAsStream ("iconasresource.gif")).); 'Su GIF no se animará –

+2

De hecho, la creación de un ImageIcon con ImageIO.read no anima el gif por algún motivo. Quizás sea obvio, pero puede obtener la URL del recurso con: 'URL url = getClass(). GetResource ("/img.gif ");'. –

+0

Lo estoy usando con Java8 y no está animando mi GIF ... –

5

Para gifs de carga animada almacenados en un paquete fuente (en el código fuente), esto funcionó para mí:

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

Esto es exactamente lo que no funciona para mí. La imagen está cargada, pero solo se muestra el primer cuadro, sin animación. – 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

¡Esto funciona para mí!

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

Es bueno agregar alguna explicación sobre su código. ¿Cómo es diferente? ¿Por qué funciona? Gracias (moderador). –

3

Vine aquí en busca de la misma respuesta, pero según las primeras respuestas, se me ocurrió un código más fácil. Espero que esto ayude en futuras búsquedas.

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

quería poner el archivo .gif en una interfaz gráfica de usuario, pero muestra con otros elementos. Y el archivo .gif se tomaría del proyecto Java y no de una URL.

1 - Parte superior de la interfaz sería una lista de elementos en la que podemos elegir uno

2 - Centro sería el GIF animado

3 - Abajo se mostrará el elemento seleccionado de la lista

Aquí está mi código (necesito 2 archivos java, la primera (Interf.java) llama a la segunda (Display.java)):

1 - Interf.java

public class Interface_for { 

    public static void main(String[] args) { 

     Display Fr = new Display(); 

    } 
} 

2 - Display.java

INFO: Sea Shure para crear una nueva carpeta de origen (NUEVA carpeta de origen>) en su proyecto de Java y poner el .gif interior para que sea visto como un archivo.

Obtengo el archivo gif con el siguiente código, por lo que puedo exportarlo en un proyecto jar (luego está animado).

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