我想用applet 实现一个很简单的动画,然后把它嵌入application中的窗口中,可是 applet单独运行的时候动画是循环出现的,但是嵌入到application时就不循环出来了还有请问下,怎么控制applet在窗口中出现的位置?我用了 setLocation(),可是不管用。。下面是两个类。请高手帮我解答一下吧 。谢谢了。。
Applet :import java.awt.*;
import java.applet.*;
public class Movewords extends Applet implements Runnable {
Thread aThread = null;
String word ="null";
boolean painted=false;
Font f = new Font("TimesRoman", Font.BOLD, 18);
int x=0,y;
Image BufferImage;
Graphics BufferGraphics;
public void init(String s) {
word=s;
this.setSize(400,300);
//this.setLocation(100,100);
BufferImage=createImage(getSize().width,getSize().height);
BufferGraphics=BufferImage.getGraphics();
}
public void start() {
if (aThread==null) {
aThread = new Thread(this);
aThread.start();
}
}
public void run() {
while(true){
if(x>=getSize().width){
x=0;}
repaint();
try {
aThread.sleep(100);
} catch (InterruptedException e) {
if(painted)
{
painted=false;
}
}
x+=3;
}
}
public void paint(Graphics g) {
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:
import java.awt.*;
import javax.swing.*;
public class TestWindow extends JFrame {
public TestWindow(){
setSize(800,600);
setLocation(0,50);
this.setVisible(true);//设置框架可显
validate();
this.setResizable(false);//设置框架不可以改变大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void launchApplet()
{
Movewords m=new Movewords();
m.setBounds(50, 60, 400, 300);//感觉这句没用的说
add(m);
m.init("Welcome lala ");
m.start();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestWindow t=new TestWindow();t.launchApplet(); }
Applet :import java.awt.*;
import java.applet.*;
public class Movewords extends Applet implements Runnable {
Thread aThread = null;
String word ="null";
boolean painted=false;
Font f = new Font("TimesRoman", Font.BOLD, 18);
int x=0,y;
Image BufferImage;
Graphics BufferGraphics;
public void init(String s) {
word=s;
this.setSize(400,300);
//this.setLocation(100,100);
BufferImage=createImage(getSize().width,getSize().height);
BufferGraphics=BufferImage.getGraphics();
}
public void start() {
if (aThread==null) {
aThread = new Thread(this);
aThread.start();
}
}
public void run() {
while(true){
if(x>=getSize().width){
x=0;}
repaint();
try {
aThread.sleep(100);
} catch (InterruptedException e) {
if(painted)
{
painted=false;
}
}
x+=3;
}
}
public void paint(Graphics g) {
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:
import java.awt.*;
import javax.swing.*;
public class TestWindow extends JFrame {
public TestWindow(){
setSize(800,600);
setLocation(0,50);
this.setVisible(true);//设置框架可显
validate();
this.setResizable(false);//设置框架不可以改变大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void launchApplet()
{
Movewords m=new Movewords();
m.setBounds(50, 60, 400, 300);//感觉这句没用的说
add(m);
m.init("Welcome lala ");
m.start();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestWindow t=new TestWindow();t.launchApplet(); }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货