问题描述
importjava.awt.FlowLayout;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.net.URL;importjavax.swing.JButton;importjavax.swing.JFrame;publicclassIconImagedemo{publicJFrameframe;publicJButtonbutton1,button2,button3,button4;publicIconImagedemo(){frame=newJFrame("图标变换");button1=newJButton("图标1");button1.addActionListener(newClick1());frame.setLayout(newFlowLayout());frame.add(button1);frame.setSize(100,100);frame.setVisible(true);frame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}publicvoidwindowOpened(WindowEvente){frame.setLocationRelativeTo(null);}});}publicclassClick1implementsActionListener{publicvoidactionPerformed(ActionEvente){Stringresourse="d:\temp\i.jpg";URLurl=getClass().getResource(resourse);frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));}}publicstaticvoidmain(Stringargs[]){newIconImagedemo();}}----------------------------------------------------------------以上是代码,本人想单击按钮后改变frame的图标,但是只要运行就会卡死,没有图标改变的动作发生.....请教各位啦
解决方案
解决方案二:
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("d:\temp\i.jpg");这样试试本地路径,URL去掉
解决方案三:
引用1楼qiaoqiao7370的回复:
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("d:\temp\i.jpg");这样试试本地路径,URL去掉
哎呦...不错哦~~~