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)
资源imageButton.png的位置不对,参照:<arel="nofollow">http://www.cnblogs.com/tqsummer/archive/2011/02/16/1955956.html
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。