update:demo11

This commit is contained in:
dichgrem
2026-01-14 16:13:53 +08:00
parent 6ce5c76843
commit 1bbfe0e546
4 changed files with 226 additions and 0 deletions

View File

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