求一个倒计时,秒表。需要JAVA. 需要有断电记忆功能

java.就是说。当程序开启的时候保存数据。下一次打开有重新读取数据。给个完整的代码。谢谢大侠了

以下是一个简单的Java倒计时和秒表程序示例,其中使用了Timer和'计时器任务TimerTask类来实现计时功能,使用了'FileFile和'FileWriterFileWriter类来实现断电记忆功能。在程序中,倒计时可以通过设置'countDownSecondscountDownSeconds变量来设置,秒表可以通过点击"开始"和"停止"按钮来控制计时。每次停止计时后,程序将自动保存当前计时的时间戳,以实现断电记忆功能。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class TimerExample extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel countDownLabel, stopWatchLabel;
private JButton countDownButton, stopWatchButton, stopButton;
private Timer countDownTimer, stopWatchTimer;
private int countDownSeconds, stopWatchSeconds;
private long lastStopWatchTime;
public TimerExample() {
super("倒计时和秒表");
setLayout(new GridLayout(3, 2, 10, 10));
// 倒计时标签和按钮
countDownLabel = new JLabel("倒计时: 00:00:00");
countDownButton = new JButton("开始倒计时");
countDownButton.addActionListener(this);
add(countDownLabel);
add(countDownButton);
// 秒表标签和按钮
stopWatchLabel = new JLabel("秒表: 00:00:00");
stopWatchButton = new JButton("开始");
stopWatchButton.addActionListener(this);
stopButton = new JButton("停止");
stopButton.addActionListener(this);
stopButton.setEnabled(false);
add(stopWatchLabel);
add(stopWatchButton);
add(stopButton);
// 初始化计时器
countDownTimer = new Timer(1000, new CountDownTask());
stopWatchTimer = new Timer(1000, new StopWatchTask());
// 恢复上一次停止的时间
try {
File file = new File("last_stopwatch_time.txt");
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
lastStopWatchTime = Long.parseLong(reader.readLine());
reader.close();
stopWatchSeconds = (int) ((System.currentTimeMillis() - lastStopWatchTime) / 1000);
updateStopWatchLabel();
}
} catch (IOException e) {
e.printStackTrace();
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
private void updateCountDownLabel() {
int hours = countDownSeconds / 3600;
int minutes = (countDownSeconds % 3600) / 60;
int seconds = countDownSeconds % 60;
countDownLabel.setText(String.format("倒计时: %02d:%02d:%02d", hours, minutes, seconds));
}
private void updateStopWatchLabel() {
int hours = stopWatchSeconds / 3600;
int minutes = (stopWatchSeconds % 3600) / 60;
int seconds = stopWatchSeconds % 60;
stopWatchLabel.setText(String.format("秒表: %02d:%02d:%02d", hours, minutes, seconds));
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == countDownButton) {
if (countDownTimer.isRunning())
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-04-20
以下是一个简单的倒计时和秒表程序的JAVA代码示例,它包括断电记忆功能,将倒计时和秒表的时间保存到文件中,下次打开程序时从文件中读取并继续计时或计数。
import java.io.*;
public class TimerAndStopwatch {
private static final String TIMER_FILE = "timer.txt";
private static final String STOPWATCH_FILE = "stopwatch.txt";
// 倒计时
public static void countdown(int seconds) throws InterruptedException {
// 读取上次保存的时间
int prevTime = readFromFile(TIMER_FILE);
if (prevTime > 0) {
seconds = prevTime;
}
System.out.println("倒计时开始:" + seconds + " 秒");
while (seconds > 0) {
Thread.sleep(1000);
seconds--;
System.out.println("剩余时间:" + seconds + " 秒");
saveToFile(TIMER_FILE, seconds);
}
System.out.println("时间到!");
}
// 秒表
public static void stopwatch() throws InterruptedException {
// 读取上次保存的时间
int prevTime = readFromFile(STOPWATCH_FILE);
System.out.println("秒表开始!");
int seconds = prevTime;
while (true) {
Thread.sleep(1000);
seconds++;
System.out.println("计时:" + seconds + " 秒");
saveToFile(STOPWATCH_FILE, seconds);
}
}
// 从文件中读取时间
private static int readFromFile(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line = reader.readLine();
if (line != null && !line.isEmpty()) {
return Integer.parseInt(line);
}
} catch (IOException e) {
System.err.println("读取文件出错:" + e.getMessage());
}
return 0;
}
// 将时间保存到文件
private static void saveToFile(String filename, int seconds) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println(seconds);
} catch (IOException e) {
System.err.println("保存文件出错:" + e.getMessage());
}
}
public static void main(String[] args) throws InterruptedException {
countdown(60);
stopwatch();
}
}
在这个程序中,我们使用了两个常量来表示保存时间的文件名,TIMER_FILE表示倒计时的文件名,STOPWATCH_FILE表示秒表的文件名。在倒计时和秒表开始时,我们读取上次保存的时间,如果有的话,将其作为初始时间;否则,使用默认时间。在计时或计数时,我们使用Thread.sleep(1000)让程序暂停1秒钟,然后更新时间或计数并保存到文件中。当程序下一次启动时,会从文件中读取保存的时间或计数,然后继续计时或计数。
第2个回答  2023-04-21
文件的读写啊,使用 FileOutputStream 类来向文件写入内容,用FileInputStream 类来读取文件内容
相似回答