Java用线程循环换获取当前时间问题!每秒刷新一次~ 求代码,~!

 我来答
百度网友0f2dc44
2012-07-04
知道答主
回答量:49
采纳率:0%
帮助的人:26.5万
展开全部
import java.awt.Toolkit;
import java.util.Calendar;

import javax.swing.*;

public class Time extends JFrame implements Runnable{
int year = 0;
int moth = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
JLabel jl1 = null;

public Time(){
Calendar c=Calendar.getInstance();//获得系统当前日期
year=c.get(Calendar.YEAR);
moth = c.get(Calendar.MONTH)+1;//系统日期从0开始算起
day=c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);//小时
minute =c.get(Calendar.MINUTE);//分
second = c.get(Calendar.SECOND);//秒
jl1 = new JLabel(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒");
jl1.setBounds(200, 100, 100, 50);
this.add(jl1);

this.setTitle("时间线程");
this.setLocation((width - 500) / 2, (height - 500) / 2);
this.setSize(500, 500);
this.setVisible(true);
}

public static void main(String[] args) {
Runnable aRunnable = new Time();
new Thread(aRunnable).start();

}
@Override
public void run() {
while(true){
try {
Calendar c=Calendar.getInstance();//获得系统当前日期
year=c.get(Calendar.YEAR);
moth = c.get(Calendar.MONTH)+1;//系统日期从0开始算起
day=c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);//小时
minute =c.get(Calendar.MINUTE);//分
second = c.get(Calendar.SECOND);//秒

jl1.setText(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}
Dubbo调试工具
2012-07-04 · TA获得超过303个赞
知道小有建树答主
回答量:173
采纳率:0%
帮助的人:105万
展开全部
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Time extends JFrame implements Runnable {

int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
JLabel jl1 = new JLabel();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");

public Time() {
jl1 = new JLabel(sdf.format(new Date()));
jl1.setBounds(200, 100, 100, 50);
this.add(jl1);
this.setTitle("时间线程");
this.setLocation((width - 500) / 2, (height - 500) / 2);
this.setSize(500, 500);
this.setVisible(true);
}

public static void main(String[] args) {
Time t = new Time();
new Thread(t).start();
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
jl1.setText(sdf.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

简洁明了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式