mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 04:21:56 -05:00
46 lines
1.1 KiB
Java
46 lines
1.1 KiB
Java
package com.study.demo11;
|
|
|
|
import java.awt.Frame;
|
|
import java.awt.Label;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
public class TimeViewer extends Frame {
|
|
private Label timeLabel;
|
|
private DateTimeFormatter formatter;
|
|
|
|
public TimeViewer() {
|
|
super("系统时间显示器");
|
|
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
timeLabel = new Label("", Label.CENTER);
|
|
add(timeLabel);
|
|
|
|
setSize(300, 100);
|
|
setLayout(new java.awt.FlowLayout());
|
|
setLocationRelativeTo(null);
|
|
setVisible(true);
|
|
|
|
updateTime();
|
|
startTimeUpdate();
|
|
}
|
|
|
|
private void updateTime() {
|
|
LocalDateTime now = LocalDateTime.now();
|
|
timeLabel.setText(now.format(formatter));
|
|
}
|
|
|
|
private void startTimeUpdate() {
|
|
new java.util.Timer().scheduleAtFixedRate(new java.util.TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
updateTime();
|
|
}
|
|
}, 0, 1000);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new TimeViewer();
|
|
}
|
|
}
|