Icon icon=new ImageIcon(url);总是报错

wy081142 发布于 2015/08/05 10:06
阅读 1K+
收藏 0
package testJButton;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import testImageIcon.MyImageIcon;


public class JButtonTest extends JFrame{
public JButtonTest(){
URL url=MyImageIcon.class.getResource("imageButton.png");
Icon icon=new ImageIcon(url);
setLayout(new GridLayout(3,2,5,5));
Container c=getContentPane();
for(int i=0;i<5;i++){
JButton J=new JButton("button"+i,icon);
c.add(J);
if(i%2==0){
J.setEnabled(false);
}
}
JButton jb=new JButton();
jb.setMaximumSize(new Dimension(90, 30));
jb.setIcon(icon);
jb.setHideActionText(true);
jb.setToolTipText("图片按钮");
jb.setBorderPainted(false);
jb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "弹出对话框");
}
});
c.add(jb);
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}


public static void main(String[] args) {
// TODO Auto-generated method stub
        new JButtonTest();
}
}



Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at testJButton.JButtonTest.<init>(JButtonTest.java:13)
at testJButton.JButtonTest.main(JButtonTest.java:44)




加载中
返回顶部
顶部