以前给别人写的
感觉差不多
改改吧
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){
}
}
}
}