use*_*488 -1 java swing nullpointerexception
好吧,基本上我尝试使用url显示.gif,但它给了我一个空指针异常,我不知道为什么,因为我的url是正确的,我的代码没有其他问题(至少没有我能看到) .
import javax.swing.*;
import java.net.*;
public class image {
public image() {
}
public static void main(String[] args) {
URL url = image.class.getResource("<http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif>");
ImageIcon imageIcon = new ImageIcon(url);
JLabel label = new JLabel(imageIcon);
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(label);
frame.add(panel);
frame.setTitle("Title");
frame.setSize(700,500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
URL url = image.class.getResource("<http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif>");
Run Code Online (Sandbox Code Playgroud)
不是您如何从Web资源引用图像.您将使用此方法加载嵌入在应用程序中的资源(在应用程序类路径的上下文中)
URL url = new URL("http://cdn.osxdaily.com/wp-content/uploads/2013/07/dancing-banana.gif");
Run Code Online (Sandbox Code Playgroud)
可能会更好......
请记住,下载和加载图像可能需要一些时间,您可能希望使用a MediaTracker来跟踪进度,这将允许您向用户提供反馈,并知道何时在图像可用时更新屏幕,例如.
在有人要求之前,我选择不使用ImageIO加载动画gif,因为这只是更多的工作(如此处所示 - 不适合胆小的人).在这种情况下,MediaTracker可以用来检查错误
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |