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

如题所述

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();
}
}

}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-07-04
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();
}
}

}
}

简洁明了

Java用线程循环换获取当前时间问题!每秒刷新一次~ 求代码,~!
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...

java中获取了当前系统地时间,怎样每秒获取更新一次呢?
楼上说得不错,我补充一点,你先开启一个线程,然后在线程里面写一个死循环,让java代码不断的去获取系统时间,注意一定要把死循环放在你单独开启的那个线程里面,不然会出错哦. 可以用while(true){代码} 这种形式来写,循环代码里面主要包含两个内容,一个是获取系统当前时间,然后让线程睡眠一秒钟,就是...

在java中,如何在标签中每秒显示一下当前系统时间
你好,这个很容易实现,开启一个子线程,不断地对标签设置当前的时间 new Thread() { public void run() { try { while (true) { jLabel1.setText(new Date().toLocaleString());\/\/显示当前时间 Thread.sleep(1000);\/\/暂停一秒 } } catch (Exception e) { } } }.start();...

求用java语言设计一个动态时钟,每秒刷新一次
import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;public class Clock { public static void main(String[] args) throws InterruptedException { JFrame f = new JFrame();Timer timer = new Timer();Thread th=new Thread(timer);th...

用java编写一个电子时钟程序。每秒显示一次当前的时间。
import java.awt.event.*;import javax.swing.*;import java.util.*;import java.util.concurrent.*;public class Clock extends JFrame implements Runnable { private JLabel label = new JLabel();public Clock() { this("Q6");label.setFont(new Font("Dialog", Font.BOLD, 72));flush()...

多线程练习 向控制台输出当前的系统时间: 要求每秒更新一次。格式:yyy...
package com.test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class PrnCurrentTime implements Runnable { public void run() { while(true){ try { Thread.sleep(5000); }

java登陆界面上如何在右下角刷新显示时间,1秒刷一次
另外开个线程啊。你可以这样,大概方法是,继承runnable接口,里面实现RUN方法,run这样写 run(){ while(true){\/\/或者这个不是true,自己设置一个标志。Thread.sleep(1000);\/\/这里加入你要干的事,比如就是一个成员变量time++;} } 最后记得让线程启动 ...

怎么在java main方法里用一个线程的sleep()方法让一个循环执行一次后阻...
public static void main(String[] args) { final Thread t1 = new Thread(){ public void run(){ int i = 0;while(i<10){ System.out.println("t1:"+i++);try { Thread.sleep(500);} catch (InterruptedException e) { e.printStackTrace();} } } };final Thread t2 = new ...

java多线程实现 5秒一次打印当前时间
import java.util.Date;public class PrintTime { public static void main(String[] args) { while (true) { System.out.println(new Date());try { Thread.sleep(5000);} catch (InterruptedException e) { e.printStackTrace();} } } } ...

java 实现一个程序每隔3S显示一下当前时间。直到键盘输入0的时候结束...
代码如下:import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;class DisplayTimeThread implements Runnable {private volatile boolean exit = false;public void exit() {this.exit = true;}@Overridepublic void run() {while (!exit) {SimpleDateFormat format = ...

相似回答