mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 04:21:56 -05:00
update:demo11
This commit is contained in:
45
com/study/demo11/TimeViewer.java
Normal file
45
com/study/demo11/TimeViewer.java
Normal file
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user