我想在java swing中添加图像,我写了一个简单的代码,但代码中没有任何错误(我也试过(" rr.png")而不是(" c:\ rr) .png")但在这两种情况下,当我想要运行它时,我得到了这个:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at quize1.uml.<init>(uml.java:14)
at quize1.uml.main(uml.java:20)
这是我的代码:
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class uml extends JFrame {
private ImageIcon image2;
private JLabel Label2;
public uml() {
setLayout(new FlowLayout());
image2 = new ImageIcon(getClass().getResource("c:\\rr.png"));
Label2 = new JLabel( image2);
add (Label2);
}
public static void main(String arg[]) {
uml gui = new uml();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setTitle("uml ");
}}