【发布时间】:2015-02-26 23:57:50
【问题描述】:
好吧,基本上我尝试使用 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);
}
}
【问题讨论】:
标签: java swing nullpointerexception