我想做个滚动字幕,但不知道错在哪,烦请大侠们指教
import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class moveWZ extends Applet implements ActionListener{
static moveWZ.thread3 wt1,wt2;
public void init(){
wt1=new Thread3("你好!");
wt2=new Thread("我在动!!!");
wt2.start();
wt2.setButton();
setLayout(new GridLayout(4,1));

}
public class Thread3 extends Thread{
Panel pl;Label lbl;TextField tf1,tf2;Button b1,b2;
int sleeptime=(int)(Math.random()*100);
public Thread3(String str){
super(str);
for(int i=0;i<100;i++)
str=str+"";
tf1=new TextField(str);
add(tf1);
pl=new Panel();
pl.setLayout(new FlowLayout(FlowLayout.LEFT));
lbl=new Label("sleep");
tf2=new TextField(""+sleeptime);
pl.add(lbl);pl.add(tf2);
b1=new Button("启动"); b2=new Button("中断");
b1.addActionListener(new moveWZ());
b2.addActionListener(new moveWZ());
add(pl);


}
public void run(){
String str;
while (this.isAlive()&&!this.isInterrupted()){
try{
str=tf1.getText();
str=str.substring(1)+str.substring(0,1);
tf1.setText(str);
this.sleep(sleeptime);
}
catch(InterruptedException e){
System.out.println(e);
break;
}
}
public void setButton(){
if(this.isAlive())
b1.setEnabled(false);
if(this.isInterrupted())
b2.setEnabled(false);
}
}
public void actionPerformed(ActionEvent e){
if((e.getSource()==wt1.b1)||(e.getSource()==wt1.b2))
actionPerformed(e,wt1);
if((e.getSource()==wt2.b1)||(e.getSource()==wt2.b2))
actionPerformed(e,wt2);

}
public void actionPerformed(ActionEvent e,Thread3 wt1){
if(e.getSource()==wt1.b1){
wt1.sleeptime=Integer.parseInt(wt1.tf2.getText());
wt1.start();
}
if(e.getSource()==wt1.b2)
wt1.interrupt();
wt1.setButton();

}

}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}



}

解决方案 »

  1.   


    package com.wht.common;import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;public class moveWZ extends Applet implements ActionListener {
    static Thread3 wt1, wt2; public void init() {
    wt1 = new Thread3("你好!");
    wt2 = new Thread3("我在动!!!");
    wt1.start();
    wt2.start();
    setLayout(new GridLayout(4, 1)); } public class Thread3 extends Thread {
    Panel pl; Label lbl; TextField tf1, tf2; Button b1, b2; int sleeptime = (int) (Math.random() * 100); public Thread3(String str) {
    super(str);
    for (int i = 0; i < 100; i++)
    str = str + "";
    tf1 = new TextField(str);
    add(tf1);
    pl = new Panel();
    pl.setLayout(new FlowLayout(FlowLayout.LEFT));
    lbl = new Label("sleep");
    tf2 = new TextField("" + sleeptime);
    pl.add(lbl);
    pl.add(tf2);
    b1 = new Button("启动");
    b2 = new Button("中断");
    b1.addActionListener(new moveWZ());
    b2.addActionListener(new moveWZ());
    add(pl); } public void run() {
    String str;
    while (this.isAlive() && !this.isInterrupted()) {
    try {
    str = tf1.getText();
    str = str.substring(1) + str.substring(0, 1);
    tf1.setText(str);
    this.sleep(sleeptime);
    } catch (InterruptedException e) {
    System.out.println(e);
    break;
    }
    }
    // public void setButton(){
    // if(this.isAlive())
    // b1.setEnabled(false);
    // if(this.isInterrupted())
    // b2.setEnabled(false);
    // }
    } public void actionPerformed(ActionEvent e) {
    if ((e.getSource() == wt1.b1) || (e.getSource() == wt1.b2))
    actionPerformed(e, wt1);
    if ((e.getSource() == wt2.b1) || (e.getSource() == wt2.b2))
    actionPerformed(e, wt2); } public void actionPerformed(ActionEvent e, Thread3 wt1) {
    if (e.getSource() == wt1.b1) {
    wt1.sleeptime = Integer.parseInt(wt1.tf2.getText());
    wt1.start();
    }
    if (e.getSource() == wt1.b2)
    wt1.interrupt();
    // wt1.setButton(); } } public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub }}
      

  2.   

    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class moveWZ extends Applet implements ActionListener {
    static Thread3 wt1, wt2; public void init() {
    wt1 = new Thread3("你好!");
    wt2 = new Thread3("我在动!!!");
    wt2.start();
    wt2.setButton();
    setLayout(new GridLayout(4, 1)); } public class Thread3 extends Thread {
    Panel pl;
    Label lbl;
    TextField tf1, tf2;
    Button b1, b2;
    int sleeptime = (int) (Math.random() * 100); public Thread3(String str) {
    super(str);
    for (int i = 0; i < 100; i++)
    str = str + "";
    tf1 = new TextField(str);
    add(tf1);
    pl = new Panel();
    pl.setLayout(new FlowLayout(FlowLayout.LEFT));
    lbl = new Label("sleep");
    tf2 = new TextField("" + sleeptime);
    pl.add(lbl);
    pl.add(tf2);
    b1 = new Button("启动");
    b2 = new Button("中断");
    b1.addActionListener(new moveWZ());
    b2.addActionListener(new moveWZ());
    add(pl); } public void run() {
    String str;
    while (this.isAlive() && !this.isInterrupted()) {
    try {
    str = tf1.getText();
    str = str.substring(1) + str.substring(0, 1);
    tf1.setText(str);
    this.sleep(sleeptime);
    } catch (InterruptedException e) {
    System.out.println(e);
    break;
    }
    }
    } public void setButton() {
    if (this.isAlive())
    b1.setEnabled(false);
    if (this.isInterrupted())
    b2.setEnabled(false);
    }
    } public void actionPerformed(ActionEvent e) {
    if ((e.getSource() == wt1.b1) || (e.getSource() == wt1.b2))
    actionPerformed(e, wt1);
    if ((e.getSource() == wt2.b1) || (e.getSource() == wt2.b2))
    actionPerformed(e, wt2); } public void actionPerformed(ActionEvent e, Thread3 wt1) {
    if (e.getSource() == wt1.b1) {
    wt1.sleeptime = Integer.parseInt(wt1.tf2.getText());
    wt1.start();
    }
    if (e.getSource() == wt1.b2)
    wt1.interrupt();
    wt1.setButton(); }}