在 JOptionPane 中放置一个 gif

Kev*_*mán 3 java swing gif joptionpane

我试图在 JOptionPane 中放置一个 Gif 图像来模拟“加载”,但没有结果。我尝试使用 URL、本地图像和图像的绝对路径,但我只能在其中放置图像(png、jpg...)。我最后一次尝试是这样

final ImageIcon icon = new ImageIcon(new URL("http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif"));
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,JOptionPane 图像的占位符看起来是空的。

所以我想知道:是否可以将 GIF 放在 JOptionPane 中,甚至放在 JFrame 中?

这是我正在使用的代码

private void cerrarsesion() throws Exception {

        this.dispose();
        String path = "/com/icono/loading.gif";  
        URL url = this.getClass().getResource(path);
        System.out.println(url);
        ImageIcon icon = new ImageIcon(url);
        createTimerClose(10).start();
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
        new Login().setVisible(true);

}
Run Code Online (Sandbox Code Playgroud)

STa*_*efi 5

这是完全可能的,您的代码必须工作。目前我正在运行您的代码,我看到以下内容:

在此处输入图片说明

我认为您在访问加载 Icon 的 URL 时遇到问题。可能是 DNS 的问题,或者您可能使用了对某些 URL 访问受限的代理。

在浏览器中尝试以下 URL:

http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif

如果您可以在浏览器中看到这个正在加载的 gif 图标,那么您必须在JOPtionPane.

如果您在浏览器中看到loading.gif图标但看不到它JOPtionPane,请尝试以下操作:

  1. 下载它并将其放入您正在编写上述代码的包中。例如,如果您有一个名为 的类Test.java,请将loading.gif文件放在与Test.java现有包相同的包中。

  2. 在main方法中编写如下代码Test.java

    ImageIcon icon = new ImageIcon(Test.class.getResource("loading.gif").getFile());
    JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
    
    Run Code Online (Sandbox Code Playgroud)

现在您应该在JOptionPane对话框中看到动画 loading.gif 。如果您现在看到 loading.gif,您应该尝试查找从您的 Java 代码访问该 URL 的问题。有时,防病毒软件或防火墙会禁止访问诸如 java.exe(您的 jvm)之类的应用程序以进行出站或入站流量。也许你的问题是这样的。

但是你的问题答案是 Yes。可以在 JOptionPanes 或 JFrames 中加载和显示 GIF 文件。

祝你好运。