问题描述
我想用applet实现一个很简单的动画,然后把它嵌入application中的窗口中,可是applet单独运行的时候动画是循环出现的,但是嵌入到application时就不循环出来了。。。还有请问下,怎么控制applet在窗口中出现的位置?我用了setLocation(),可是不管用。。。。。下面是两个类。。。。请高手帮我解答一下吧。。。。谢谢了。。。。。Applet:importjava.awt.*;importjava.applet.*;publicclassMovewordsextendsAppletimplementsRunnable{ThreadaThread=null;Stringword="null";booleanpainted=false;Fontf=newFont("TimesRoman",Font.BOLD,18);intx=0,y;ImageBufferImage;GraphicsBufferGraphics;publicvoidinit(Strings){word=s;this.setSize(400,300);//this.setLocation(100,100);BufferImage=createImage(getSize().width,getSize().height);BufferGraphics=BufferImage.getGraphics();}publicvoidstart(){if(aThread==null){aThread=newThread(this);aThread.start();}}publicvoidrun(){while(true){if(x>=getSize().width){x=0;}repaint();try{aThread.sleep(100);}catch(InterruptedExceptione){if(painted){painted=false;}}x+=3;}}publicvoidpaint(Graphicsg){BufferGraphics.setColor(Color.white);BufferGraphics.fillRect(0,0,getSize().width,getSize().height);BufferGraphics.setColor(Color.red);BufferGraphics.fillOval(x,20,20,20);BufferGraphics.setFont(f);BufferGraphics.drawString(word,x,50);painted=true;g.drawImage(BufferImage,0,0,this);}}Application:importjava.awt.*;importjavax.swing.*;publicclassTestWindowextendsJFrame{publicTestWindow(){setSize(800,600);setLocation(0,50);this.setVisible(true);//设置框架可显validate();this.setResizable(false);//设置框架不可以改变大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidlaunchApplet(){Movewordsm=newMovewords();m.setBounds(50,60,400,300);//感觉这句没用的说add(m);m.init("Welcomelala");m.start();}/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubTestWindowt=newTestWindow();t.launchApplet();}
解决方案
解决方案二:
关于applet的问题太难得一见了。不会啊,友情帮顶。
解决方案三:
applet,我只在书上见过,还跳过这一段了。。。。