多线程问题,同步显示时间
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
class timeframe extends JFrame implements Runnable {
Container pane = getContentPane();
Label L1 = new Label();
Label L2 = new Label();
Label L3 = new Label();
String hour,minute,second;
int flag = 0;
public timeframe() {
Thread timethread = new Thread();
Date date = new Date();
hour = String.valueOf(date.getHours());
minute = String.valueOf(date.getMinutes());
second = String.valueOf(date.getSeconds());
pane.add(L1,BorderLayout.WEST);
L1.setText(hour+ ": "+minute+ ": "+second);
pane.add(L2,BorderLayout.CENTER);
L2.setText(hour+ ": "+minute+ ": "+second);
pane.add(L3,BorderLayout.EAST);
L3.setText(hour+ ": "+minute+ ": "+second);
timethread.start();
}
public void run() {
while(true){
try {
Date date = new Date();
hour = String.valueOf(date.getHours());
minute = String.valueOf(date.getMinutes());
second = String.valueOf(date.getSeconds());
flag++;
L1.setText(hour+ ": "+minute+ ": "+second);
if(flag % 5 == 0)
L2.setText(hour+ ": "+minute+ ": "+second);
if(flag % 10 == 0)
L3.setText(hour+ ": "+minute+ ": "+second);
Thread.sleep(1000);
}catch(InterruptedException e) {}
}}
public static void main(String[] args){
timeframe time1 = new timeframe();
time1.pack();
time1.show();
}
}
就显示三个时间标签的
为什么我的显示的时间总是初始化时的那个时间的
大家帮帮忙了
------解决方案--------------------首先你的代码用了在大堆已经过期的方法,全部应该修改
其次,这个程序不会退出的
你的问题:
Thread timethread = new Thread(this);这样才可以。