运行一下这个程序:(感觉是对的,怎么IE闪一下就没了啊?)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class number extends Applet implements Runnable,ActionListener
{
Button button;
Thread thread1,thread2;
TextField text1,text2;
public void init()
{
text1=new TextField(20);
text2=new TextField(20);
add(text1);
add(text2);
button=new Button("starts thread");
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
thread1=new Thread(this,"threadname1");
thread1.start();
thread2=new Thread(this,"threadname2");
thread2.start();
}
}
public void run()
{
String s=new String();
int i;
for(i=0;i<1000;i++)
{
if(((Thread.currentThread()).getName()).equals("threadname1"))
text1.setText(s.valueOf(i));
else
if(((Thread.currentThread()).getName()).equals("threadname1"))
text2.setText(s.valueOf(i));
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class number extends Applet implements Runnable,ActionListener
{
Button button;
Thread thread1,thread2;
TextField text1,text2;
public void init()
{
text1=new TextField(20);
text2=new TextField(20);
add(text1);
add(text2);
button=new Button("starts thread");
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
thread1=new Thread(this,"threadname1");
thread1.start();
thread2=new Thread(this,"threadname2");
thread2.start();
}
}
public void run()
{
String s=new String();
int i;
for(i=0;i<1000;i++)
{
if(((Thread.currentThread()).getName()).equals("threadname1"))
text1.setText(s.valueOf(i));
else
if(((Thread.currentThread()).getName()).equals("threadname1"))
text2.setText(s.valueOf(i));
}
}
}
text2=new TextField(5);
好像可以了,以前我也没遇到过,有点奇怪。
程序还有一个地方有问题,刚才没发现:
else if(((Thread.currentThread()).getName()).equals("threadname1"))改为:
else if(((Thread.currentThread()).getName()).equals("threadname2"))