我改过的代码
/*
* @(#)E1504.java 1.0 03/05/11
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
package myprojects.e1504;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;public class E1504 extends JFrame
{
int sleepTime;//线程的休息时间
char ch[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private Container c;
private JTextField tf[] = new JTextField[3];
private static int count = 0;
private static Random r = new Random();
public E1504()
{
super("E1504.java");
c = getContentPane();
c.setLayout(new FlowLayout());
for(int i=0;i<3;i++)
{
tf[i] = new JTextField(20);
c.add(tf[i]);
}
setSize(200,100);
show();
}
public Runnable getRunnable()
{
return new MyRunnable();
}
class MyRunnable implements Runnable
{
private int index;
public MyRunnable()
{
index=count++;
}
public void run()
{
int i;
while(true)
{
i = r.nextInt(1000) ;
sleepTime = i;//随机的线程休息时间
System.out.println(i);
try
{
Thread.currentThread().sleep(sleepTime);
}
catch(InterruptedException e)
{
System.err.println("Exception=" +e.toString());
}
if(index==0)
tf[0].setText("执行绪1:"+ ch[i%26]);
else if(index==1)
tf[1].setText("执行绪2:"+ ch[i%26]);
else if (index==2)
tf[2].setText("执行绪3:"+ ch[i%26]);
}
}
}
public static void main(String args[])
{
E1504 f = new E1504();
Runnable Mythread[]=new Runnable[3];//三个线程
for (int i = 0; i< 3; i++)
{
Mythread[i] = f.getRunnable();
}
new Thread(Mythread[0]).start();
new Thread(Mythread[1]).start();
new Thread(Mythread[2]).start();
}
}
/*
* @(#)E1504.java 1.0 03/05/11
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
package myprojects.e1504;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;public class E1504 extends JFrame
{
int sleepTime;//线程的休息时间
char ch[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private Container c;
private JTextField tf[] = new JTextField[3];
private static int count = 0;
private static Random r = new Random();
public E1504()
{
super("E1504.java");
c = getContentPane();
c.setLayout(new FlowLayout());
for(int i=0;i<3;i++)
{
tf[i] = new JTextField(20);
c.add(tf[i]);
}
setSize(200,100);
show();
}
public Runnable getRunnable()
{
return new MyRunnable();
}
class MyRunnable implements Runnable
{
private int index;
public MyRunnable()
{
index=count++;
}
public void run()
{
int i;
while(true)
{
i = r.nextInt(1000) ;
sleepTime = i;//随机的线程休息时间
System.out.println(i);
try
{
Thread.currentThread().sleep(sleepTime);
}
catch(InterruptedException e)
{
System.err.println("Exception=" +e.toString());
}
if(index==0)
tf[0].setText("执行绪1:"+ ch[i%26]);
else if(index==1)
tf[1].setText("执行绪2:"+ ch[i%26]);
else if (index==2)
tf[2].setText("执行绪3:"+ ch[i%26]);
}
}
}
public static void main(String args[])
{
E1504 f = new E1504();
Runnable Mythread[]=new Runnable[3];//三个线程
for (int i = 0; i< 3; i++)
{
Mythread[i] = f.getRunnable();
}
new Thread(Mythread[0]).start();
new Thread(Mythread[1]).start();
new Thread(Mythread[2]).start();
}
}
可是我仿照你的代码重新改了我的代码,编译通过,运行还是不行?在帮我看看好吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class e1504 extends JFrame
{
int sleepTime;//线程的休息时间
char ch[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private Container c;
private JTextField tf[] = new JTextField[3];
//
private static int count = 0; //统计共生成了几个实例
//
public e1504()
{
super("e1504.java");
c = getContentPane();
c.setLayout(new FlowLayout());
for(int i=0;i<3;i++)
{
c.add(tf[i]);
}
setSize(200,100);
show();
}
public Runnable getRunnable()
{
return new MyRunnable();
}
class MyRunnable implements Runnable
{
private int index;
public MyRunnable()
{
index = count++;
}
public void run()
{
int i;
i = (int) Math.random()*100 ;
sleepTime = i;//随机的线程休息时间.
while(true)
{
try
{
Thread.currentThread().sleep(sleepTime);
}
catch(InterruptedException e)
{
System.err.println("Exception=" +e.toString());
}
if(index ==0)
tf[0].setText("执行绪1:"+ ch[i%26]);
else if(index ==1)
tf[1].setText("执行绪2:"+ ch[i%26]);
else if (index ==2)
tf[2].setText("执行绪3:"+ ch[i%26]);
}
}
}
public static void main(String args[])
{
e1504 app = new e1504();
Runnable myRunnable[] = new Runnable[3];
for(int i=0;i<3;i++)
{
myRunnable[i]= app.getRunnable();
}
new Thread(myRunnable[0]).start();
new Thread(myRunnable[1]).start();
new Thread(myRunnable[2]).start();
}
}