diff --git a/com/study/demo11/CountdownTimer.java b/com/study/demo11/CountdownTimer.java new file mode 100644 index 0000000..69c83a6 --- /dev/null +++ b/com/study/demo11/CountdownTimer.java @@ -0,0 +1,63 @@ +package com.study.demo11; + +import java.awt.Button; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.Label; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Timer; +import java.util.TimerTask; + +public class CountdownTimer extends Frame { + private Button sendButton; + private Label statusLabel; + private int remainingSeconds = 10; + private Timer timer; + + public CountdownTimer() { + super("按钮控制器"); + + sendButton = new Button("发送验证码"); + statusLabel = new Label("", Label.CENTER); + + sendButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + startCountdown(); + } + }); + + setLayout(new FlowLayout()); + add(sendButton); + add(statusLabel); + + setSize(300, 150); + setLocationRelativeTo(null); + setVisible(true); + } + + private void startCountdown() { + sendButton.setEnabled(false); + remainingSeconds = 10; + + timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + remainingSeconds--; + if (remainingSeconds > 0) { + statusLabel.setText("剩余时间:" + remainingSeconds + "秒"); + } else { + timer.cancel(); + sendButton.setEnabled(true); + statusLabel.setText("可以重新发送"); + } + } + }, 1000, 1000); + } + + public static void main(String[] args) { + new CountdownTimer(); + } +} diff --git a/com/study/demo11/EventStatistics.java b/com/study/demo11/EventStatistics.java new file mode 100644 index 0000000..83a6218 --- /dev/null +++ b/com/study/demo11/EventStatistics.java @@ -0,0 +1,70 @@ +package com.study.demo11; + +import java.awt.Button; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.Label; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Timer; +import java.util.TimerTask; + +public class EventStatistics extends Frame { + private Button clickButton; + private Label countLabel; + private Label periodLabel; + private int clickCount = 0; + private DateTimeFormatter formatter; + + public EventStatistics() { + super("事件统计器"); + formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + clickButton = new Button("点击"); + countLabel = new Label("点击次数:0", Label.CENTER); + periodLabel = new Label("统计周期:5秒", Label.CENTER); + + clickButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + incrementClickCount(); + } + }); + + setLayout(new FlowLayout()); + add(clickButton); + add(countLabel); + add(periodLabel); + + setSize(300, 150); + setLocationRelativeTo(null); + setVisible(true); + + startStatisticsTimer(); + } + + private void incrementClickCount() { + clickCount++; + countLabel.setText("点击次数:" + clickCount); + } + + private void startStatisticsTimer() { + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + LocalDateTime now = LocalDateTime.now(); + String statistics = "过去 5 秒内点击次数:" + clickCount; + System.out.println(statistics); + System.out.println("统计时间点:" + now.format(formatter)); + clickCount = 0; + countLabel.setText("点击次数:0"); + } + }, 5000, 5000); + } + + public static void main(String[] args) { + new EventStatistics(); + } +} diff --git a/com/study/demo11/TimeLogger.java b/com/study/demo11/TimeLogger.java new file mode 100644 index 0000000..3a73b55 --- /dev/null +++ b/com/study/demo11/TimeLogger.java @@ -0,0 +1,48 @@ +package com.study.demo11; + +import java.awt.Button; +import java.awt.Frame; +import java.awt.TextArea; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class TimeLogger extends Frame { + private Button logButton; + private TextArea logArea; + private DateTimeFormatter formatter; + + public TimeLogger() { + super("时间记录器"); + formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + logButton = new Button("记录时间"); + logArea = new TextArea(10, 40); + logArea.setEditable(false); + + logButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + logTime(); + } + }); + + add(logButton, java.awt.BorderLayout.NORTH); + add(logArea, java.awt.BorderLayout.CENTER); + + setSize(400, 300); + setLocationRelativeTo(null); + setVisible(true); + } + + private void logTime() { + LocalDateTime now = LocalDateTime.now(); + String timeStr = "点击时间:" + now.format(formatter) + "\n"; + logArea.append(timeStr); + } + + public static void main(String[] args) { + new TimeLogger(); + } +} diff --git a/com/study/demo11/TimeViewer.java b/com/study/demo11/TimeViewer.java new file mode 100644 index 0000000..c0feed6 --- /dev/null +++ b/com/study/demo11/TimeViewer.java @@ -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(); + } +}