java里如何实现动态时间

来源:百度知道 编辑:UC知道 时间:2024/05/25 01:27:01
用多线程来实现 要取得当前时间
直接在JFrame里面画个JLabel 让他1秒1更新
要是给代码的话 要带上注释哈

import java.awt.Font;
import java.util.Date;

import javax.swing.*;

public class ListTime extends JFrame implements Runnable {
JLabel lbl = null;

ListTime() {
setLayout(null);

setTitle("时间显示");
setBounds(200, 200, 400, 300);

lbl = new JLabel("");
lbl.setBounds(20, 20, 300, 200);
lbl.setFont(new Font("Arial", Font.BOLD, 20));
add(lbl);
setVisible(true);
}

public void run() {
while (true) {
try {
Thread.sleep(1000);
lbl.setText(new Date().toString());
} catch (InterruptedException e) {

}

}
}

public static void main(String[] args) {
ListTime f = new ListTime();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread myThread = new Thread(f);
myThread.start();

}

}