我试图将 Gif 图像放在 JOptionPane 中以模拟“加载”,但没有结果。 我尝试使用 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);
当我运行这段代码时,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);
}
最佳答案
这是完全可能的,您的代码必须能够正常工作。目前我正在运行您的代码,我看到以下内容:
我认为您访问加载图标的 URL 时遇到问题。可能是 DNS 的问题,也可能是您使用的代理对某些 URL 的访问受限。
在浏览器中尝试以下 URL:
http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif
如果您可以在浏览器中看到这个正在加载的 gif 图标,那么您必须在 JOPtionPane
中看到它。
如果您在浏览器中看到 loading.gif 图标,但在 JOPtionPane
中看不到它,请尝试以下操作:
下载它并将其放入您正在编写上述代码的包中。例如,如果您有一个名为
Test.java
的类,请将 loading.gif 文件放在与Test.java
相同的包中。在
Test.java
的main方法中编写如下代码:ImageIcon icon = new ImageIcon(Test.class.getResource("loading.gif").getFile()); JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
现在您应该在 JOptionPane
对话框中看到动画 loading.gif。如果您现在看到 loading.gif,您应该尝试找出从您的 Java 代码访问该 URL 的问题。有时,防病毒软件或防火墙会禁止访问 java.exe(您的 jvm)等应用程序以进行出站或入站流量。也许你的问题是这样的。
但是您的问题的答案是肯定的。可以在 JOptionPanes 或 JFrames 中加载和显示 GIF 文件。
祝你好运。
关于java - 在 JOptionPane 中放一个 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30986683/