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