问题描述
为什么下面写的代码不能实现一个名为m.gif的图像从左到右运动。感谢指教。importjava.awt.*;importjava.awt.event.*;importjava.awt.Graphics.*;importjava.awt.Toolkit;importjavax.swing.JFrame;publicclassBPFrameextendsFrame{publicImageimg=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("m.gif"));publicImageimg2=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("w.gif"));publicImageimg3=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("mx.gif"));publicImageimg4=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("wx.gif"));publicstaticintwidth=Toolkit.getDefaultToolkit().getScreenSize().width;publicstaticintheight=Toolkit.getDefaultToolkit().getScreenSize().height;publicFramef=newFrame();publicMenuItemmi1=newMenuItem("Start");publicMenum1=newMenu("Option");publicMenuBarmb=newMenuBar();//staticMenum1=newMenu("Option");publicBPFrame(){mi1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){moving(e);//System.out.println("fuck");}});//this.f.setSize(width,height);//this.f.setResizable(false);//this.f.setVisible(true);this.m1.add(this.mi1);this.mb.add(this.m1);Frameframe=newFrame();frame.setMenuBar(this.mb);frame.setVisible(true);frame.setSize(width,height);}voidmoving(ActionEvente){ImagebufferImage=this.createImage(width,height);GraphicsbG=bufferImage.getGraphics();Graphicsg=this.getGraphics();inty=300;intx=0;intw=bufferImage.getWidth(this);inth=bufferImage.getHeight(this);for(;x<width;){x=x+5;bG.clearRect(0,0,w,h);bG.drawImage(img,x,y,null);g.drawImage(bufferImage,0,0,null);try{Thread.currentThread();Thread.sleep(20);}catch(Exceptionex){}}}publicstaticvoidmain(String[]args){//MenuBarmb=newMenuBar();//Menum1=newMenu("Option");//Menum2=newMenu("Exit");//MenuItemmi1=newMenuItem("Start");/*MenuItemmi1-start8*///mi1.addActionListener(newActionListener(){//publicvoidactionPerformed(ActionEvente){//moving(e);//System.out.println("fuck");//}//});*//*MenuItemmi2*//*MenuItemmi2=newMenuItem("Stop");mi2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});/*MenuItemmi2*///MenuItemmi3=newMenuItem("Exit");//mi3.addActionListener(newActionListener(){//publicvoidactionPerformed(ActionEvente){//System.exit(0);//}//});//mb.add(m1);//mb.add(m2);//m1.add(BPFrame.mi1);//m1.add(mi2);//m2.add(mi3);newBPFrame();//frame.addWindowListener(newWindowAdapter(){//addWindowListener//publicvoidwindowClosing(WindowEvente){//System.exit(0);//}//});//frame.setMenuBar(mb);//frame.setVisible(true);//frame.setSize(width,height);//frame.setResizable(false);//frame.setMenuBar(mb);}}
解决方案
解决方案二:
看来GUI还是挺受人欢迎的,还有搞这方面的,我是不会了,帮顶。
解决方案三:
请学会用swing,awt的很多组件被swing代替了importjava.awt.*;importjava.awt.event.*;importjava.awt.Graphics.*;importjava.awt.Toolkit;importjavax.swing.JFrame;publicclassBPFrame{publicImageimg=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("2_guest.jpg"));publicImageimg2=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("2_guest.jpg"));publicImageimg3=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("2_guest.jpg"));publicImageimg4=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("2_guest.jpg"));publicstaticintwidth=Toolkit.getDefaultToolkit().getScreenSize().width;publicstaticintheight=Toolkit.getDefaultToolkit().getScreenSize().height;publicFramef=newFrame();publicMenuItemmi1=newMenuItem("Start");publicMenum1=newMenu("Option");publicMenuBarmb=newMenuBar();privateFrameframe=null;publicBPFrame(){mi1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){moving(e);}});this.m1.add(this.mi1);this.mb.add(this.m1);frame=newFrame();frame.setMenuBar(this.mb);frame.setVisible(true);frame.setSize(width,height);}voidmoving(ActionEvente){ImagebufferImage=frame.createImage(width,height);GraphicsbG=bufferImage.getGraphics();Graphicsg=frame.getGraphics();inty=300;intx=0;intw=bufferImage.getWidth(frame);inth=bufferImage.getHeight(frame);for(;x<width;){x=x+5;bG.clearRect(0,0,w,h);bG.drawImage(img,x,y,null);g.drawImage(bufferImage,0,0,null);try{Thread.currentThread();Thread.sleep(20);}catch(Exceptionex){}}}publicstaticvoidmain(String[]args){newBPFrame();}}
解决方案四:
publicImageimg=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("m.gif"));--看看你的图像文件路径是否正确?遇到问题设断点调试,看走到哪一步报错的