求各位高手帮忙把代码给弄出来!不胜感激
编写3个线程分别各显示各自的运行时间,第1个线程每隔1秒运行1次,第2个每隔5秒运行1次,第3个线程每隔10秒运行1次.
----------------解决方案--------------------------------------------------------
以前给别人写的
感觉差不多
改改吧
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class Display extends JFrame{
private JLabel tipLabel,timeText,timeText1,timeText2;
private Container container;
public Display (){
container = getContentPane();
container.setLayout(new GridLayout(4,1,5,5));
tipLabel = new JLabel("各线程已运行时间:");
timeText = new JLabel();
timeText1 = new JLabel();
timeText2 = new JLabel();
container.add(tipLabel);
container.add(timeText);
container.add(timeText1);
container.add(timeText2);
try{
Thread thread = new TimeDisplay(timeText,1);
thread.start();
Thread.sleep(1000);
Thread thread1 = new TimeDisplay(timeText1,2);
thread1.start();
Thread.sleep(1000);
Thread thread2 = new TimeDisplay(timeText2,3);
thread2.start();
Thread.sleep(1000);
}
catch(Exception e){
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack();
setSize(300,200);
setVisible(true);
//将窗口屏幕居中
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension frameSize = getSize(),screenSize = kit.getScreenSize();
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
public static void main(String args[]){
Display display = new Display();
}
}
class TimeDisplay extends Thread {
private JLabel timeField;
private long start;
private int number;
public TimeDisplay(JLabel field,int num) {
timeField = field;
number = num;
start = System.nanoTime();
}
public void run() {
while(true){
timeField.setText("线程"+number+"已经运行了"+String.valueOf((System.nanoTime()-start)/1000000000)+"秒");
try{
Thread.sleep(1000);
}
catch(Exception e){
}
}
}
}
----------------解决方案--------------------------------------------------------
回复 2# freish 的帖子
非常感谢了!!! ----------------解决方案--------------------------------------------------------
不错不错
----------------解决方案--------------------------------------------------------