补全这段代码import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Timer; import java.util.TimerTask; public class RestReminder extends JFrame { private JLabel timeLabel; private JButton setButton; private JButton resetButton; private JTextField restTimeField; private Timer timer; public RestReminder() { // 初始化界面和组件 // 添加事件监听器 // 实现设置休息时间和提醒时间的功能 } public static void main(String[] args) { RestReminder reminder = new RestReminder(); reminder.setVisible(true); } }
时间: 2024-04-02 13:35:57 浏览: 82
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
public class RestReminder extends JFrame {
private JLabel timeLabel;
private JButton setButton;
private JButton resetButton;
private JTextField restTimeField;
private Timer timer;
private int restTime; // 休息时间,单位为分钟
public RestReminder() {
// 初始化界面和组件
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("休息提醒");
setSize(300, 200);
setLocationRelativeTo(null);
timeLabel = new JLabel("00:00");
timeLabel.setFont(new Font("宋体", Font.BOLD, 40));
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
setButton = new JButton("设置");
resetButton = new JButton("重置");
restTimeField = new JTextField();
restTimeField.setColumns(10);
JPanel panel = new JPanel(new GridLayout(3, 1));
panel.add(timeLabel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(setButton);
buttonPanel.add(resetButton);
panel.add(buttonPanel);
JPanel restTimePanel = new JPanel(new FlowLayout());
restTimePanel.add(new JLabel("休息时间(分钟):"));
restTimePanel.add(restTimeField);
panel.add(restTimePanel);
add(panel);
// 添加事件监听器
setButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
restTime = Integer.parseInt(restTimeField.getText());
startTimer();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(RestReminder.this,
"请输入正确的休息时间(整数)", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopTimer();
timeLabel.setText("00:00");
}
});
}
// 实现设置休息时间和提醒时间的功能
private void startTimer() {
if (timer != null) {
stopTimer();
}
int delay = 1000; // 延迟1秒钟开始执行任务
int period = 60 * 1000; // 每隔1分钟执行一次任务
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int time = restTime;
@Override
public void run() {
if (time <= 0) {
JOptionPane.showMessageDialog(RestReminder.this,
"休息时间到了,请休息一下!", "休息提醒", JOptionPane.INFORMATION_MESSAGE);
time = restTime;
}
int minute = time / 60;
int second = time % 60;
timeLabel.setText(String.format("%02d:%02d", minute, second));
time--;
}
}, delay, period);
}
private void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
public static void main(String[] args) {
RestReminder reminder = new RestReminder();
reminder.setVisible(true);
}
}
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)