-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerPanel.java
More file actions
68 lines (58 loc) · 1.99 KB
/
TimerPanel.java
File metadata and controls
68 lines (58 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TimerPanel extends JPanel {
private Timer timer;
private int seconds = 25 * 60; // 25-minute timer
private JLabel timeLabel;
private JButton startBtn, pauseBtn, resetBtn;
private boolean running = false;
public TimerPanel() {
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
setBorder(BorderFactory.createTitledBorder("Pomodoro Timer"));
timeLabel = new JLabel(formatTime(seconds));
timeLabel.setFont(new Font("Consolas", Font.BOLD, 36));
startBtn = new JButton("Start");
pauseBtn = new JButton("Pause");
resetBtn = new JButton("Reset");
startBtn.addActionListener(e -> startTimer());
pauseBtn.addActionListener(e -> pauseTimer());
resetBtn.addActionListener(e -> resetTimer());
add(timeLabel);
add(startBtn);
add(pauseBtn);
add(resetBtn);
}
private void startTimer() {
if (!running) {
running = true;
timer = new Timer(1000, e -> {
seconds--;
timeLabel.setText(formatTime(seconds));
if (seconds <= 0) {
timer.stop();
running = false;
JOptionPane.showMessageDialog(this, "Pomodoro complete! Time for a break.");
}
});
timer.start();
}
}
private void pauseTimer() {
if (running && timer != null) {
timer.stop();
running = false;
}
}
private void resetTimer() {
if (timer != null) timer.stop();
seconds = 25 * 60;
running = false;
timeLabel.setText(formatTime(seconds));
}
private String formatTime(int totalSeconds) {
int mins = totalSeconds / 60;
int secs = totalSeconds % 60;
return String.format("%02d:%02d", mins, secs);
}
}