每日收货

package Calculation;

import javax.swing.;
import java.awt.
;
import java.util.Timer;
import java.util.TimerTask;

public class TimeFrame extends JPanel {
private JLabel timeLabel; // 显示时间
private Timer timer; // 计时器
private int seconds; // 秒数

public TimeFrame() {
    seconds = 0;
    timeLabel = new JLabel("用时:0秒", SwingConstants.CENTER);
    timeLabel.setFont(new Font("宋体", Font.BOLD, 16));
    add(timeLabel);
}

// 启动计时器
public void startTimer() {
    timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            seconds++;
            // 更新时间显示(格式:分:秒)
            int minutes = seconds / 60;
            int secs = seconds % 60;
            timeLabel.setText(String.format("用时:%d分%d秒", minutes, secs));
        }
    }, 1000, 1000); // 1秒后开始,每1秒执行一次
}

// 停止计时器
public void stopTimer() {
    if (timer != null) {
        timer.cancel();
    }
}

}

posted @ 2025-11-03 23:29  鲁国石玉峰  阅读(6)  评论(0)    收藏  举报