JButton btn = new JButton ("d:\1.jpg") ;
Graphics g = btn.getGraphics() ;
g.drawString("aaa",0,0) ;
this.add(btn) ;
JFrame frame = new JFrame () ;
frame.add(this) ;
frame.setSize(50,80) ;
frame.setVisible(true) ;
目的是想着在JButton上面画图,然后用画笔在上面写字符串。
至于为什么想着这样做,是想用用JButton里面的这个getgraphics的方法。
不过按照上面这样做会报错,空指针的错误,这是为什么呢?不能这样做吗?