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