2010-05-29 51 views

Trả lời

29

Sử dụng đu bạn chỉ có thể sử dụng một 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

Đối với một số lý do, nếu bạn nhận được 'đối tượng ImageIcon' của bạn với một cái gì đó như thế này biểu tượng' Biểu tượng = IMAGEtrong mới (ImageIO.read (getClass() getResourceAsStream ("iconasresource.gif")).); 'Của bạn GIF sẽ không hoạt ảnh –

+2

Thật vậy, việc tạo một ImageIcon bằng ImageIO.read sẽ không tạo hiệu ứng gif cho một số lý do. Có thể hiển nhiên, nhưng bạn có thể lấy URL của tài nguyên bằng: 'URL url = getClass(). GetResource ("/img.gif ");'. –

+0

Tôi đang sử dụng nó với Java8 và nó không hoạt hình GIF của tôi ... –

5

Đối với gifs tải hoạt hình được lưu trữ trong một gói nguồn (trong mã nguồn), điều này làm việc cho tôi:

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

Đây chính xác là những gì không hiệu quả đối với tôi. Hình ảnh được tải, nhưng chỉ khung hình đầu tiên được hiển thị, không có hình động. – 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

Tác phẩm này dành cho tôi!

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

Bạn nên thêm một số giải thích về mã của mình. Làm thế nào là nó khác nhau, tại sao nó làm việc. Cảm ơn (người kiểm duyệt). –

3

Tôi đến đây tìm kiếm câu trả lời tương tự, nhưng dựa trên câu trả lời hàng đầu, tôi đã đưa ra một mã dễ dàng hơn. Hy vọng điều này sẽ giúp tìm kiếm trong tương lai.

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

tôi muốn đặt file .gif trong một GUI nhưng hiển thị với các yếu tố khác. Và tệp .gif sẽ được lấy từ dự án java chứ không phải từ một URL.

1 - Đầu giao diện sẽ là một danh sách các yếu tố mà chúng ta có thể chọn một

2 - Trung tâm sẽ là GIF động

3 - dưới sẽ hiển thị các yếu tố được lựa chọn từ danh sách

đây là mã của tôi (tôi cần 2 file java, là người đầu tiên (Interf.java) gọi thứ hai (Display.java)):

1 - Interf.java

public class Interface_for { 

    public static void main(String[] args) { 

     Display Fr = new Display(); 

    } 
} 

2 - Display.java

infos: Hãy shure để tạo ra một thư mục nguồn mới (NEW> Thư mục nguồn) trong dự án java của bạn và đặt .gif bên trong cho nó được xem như là một tập tin.

Tôi nhận tệp gif với mã bên dưới, vì vậy tôi có thể xuất tệp đó trong một dự án jar (sau đó là hoạt ảnh).

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()); 
     } 
    } 
} 
Các vấn đề liên quan