【问题标题】:how to load animated gif to jpanel from mac [closed]如何从mac将动画gif加载到jpanel [关闭]
【发布时间】:2014-03-06 09:33:22
【问题描述】:

我想将动画 gif 加载到 jpanel,所以我阅读了如何做,但是当我运行程序时

我收到这条消息:

java.net.MalformedURLException: no protocol: /Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif

我检查了我的 gif 路径,它是正确的!!

那我该怎么办?

我的代码:

URL url;
            try {
                url = new URL("/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif");
                Icon imgGif = new ImageIcon(url);
                JLabel lblGif = new JLabel();
                lblGif.setIcon(imgGif);
                lblGif.setBounds(100, 100, 400, 450);
                switchPanel.add(lblGif);
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

无论如何我都解决了 :)

【问题讨论】:

  • 您可以尝试将 file:// 添加到 URI 吗?
  • URL 的 Javadocs 的哪一部分让您感到困惑?这不是 URL,因为 .. 里面没有协议。
  • 是..仍然没有帮助@mig-25foxbat
  • Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。

标签: java macos swing jpanel animated-gif


【解决方案1】:

解决了:

String path = "/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif";

                Icon imgGif = new ImageIcon(path);
                JLabel lblGif = new JLabel();
                lblGif.setIcon(imgGif);
                lblGif.setBounds(150,150, 455, 170);
                switchPanel.add(lblGif);

【讨论】:

  • 部署时,这些资源可能会变成embedded-resource。在这种情况下,资源必须由URL 而不是File 访问。请参阅标签的info page,了解形成URL 的方法。
【解决方案2】:
       Try This 
       import java.awt.*;
       import javax.swing.JFrame;
       import javax.swing.JPanel;
       import java.awt.BorderLayout;
       import javax.swing.JLabel;
       import javax.swing.ImageIcon;

class MainFrame extends JFrame {
   JPanel contentPane;
   JLabel imageLabel = new JLabel();
   JLabel headerLabel = new JLabel();

public MainFrame() {
    try {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());
        setSize(new Dimension(400, 300));
        headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
        headerLabel.setText("Animated Image in java");
        contentPane.add(headerLabel, java.awt.BorderLayout.NORTH);
        ImageIcon ii = new ImageIcon(this.getClass().getResource(
                "activity.gif"));
        imageLabel.setIcon(ii);
        contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
        // show it
        this.setLocationRelativeTo(null);
        this.setVisible(true);
     } 
    catch (Exception exception) {
        exception.printStackTrace();
    }
}

public static void main(String[] args) {
    new MainFrame();
}

}

在这里你可以看到框架内的 gif 图像

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多