-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopwatchSwing.java
105 lines (86 loc) · 3.08 KB
/
StopwatchSwing.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StopwatchSwing implements ActionListener
{
private JFrame frame;
private JButton startButton;
private JButton stopButton;
private JButton resetButton;
private JLabel elapsedTimeLabel;
private long startTime;
private long elapsedTime;
private boolean running;
private Timer timer;
public StopwatchSwing() {
// Initialize the stopwatch components
startButton = new JButton("Start");
stopButton = new JButton("Stop");
resetButton = new JButton("Reset");
elapsedTimeLabel = new JLabel("0.000 seconds", SwingConstants.CENTER);
elapsedTimeLabel.setFont(new Font("SansSerif", Font.BOLD, 32));
// Set the layout and size of the stopwatch window
frame = new JFrame("Stopwatch");
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(400, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel to hold the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
buttonPanel.add(resetButton);
// Add the buttons and elapsed time label to the stopwatch window
frame.add(buttonPanel, BorderLayout.NORTH);
frame.add(elapsedTimeLabel, BorderLayout.CENTER);
// Add action listeners to the buttons
startButton.addActionListener(this);
stopButton.addActionListener(this);
resetButton.addActionListener(this);
// Initialize the stopwatch variables
startTime = 0;
elapsedTime = 0;
running = false;
// Initialize the timer
timer = new Timer(100, this);
timer.setInitialDelay(0);
// Pack and display the stopwatch window
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
startTime = System.currentTimeMillis();
running = true;
timer.start();
}
else if (e.getSource() == stopButton)
{
elapsedTime += System.currentTimeMillis() - startTime;
running = false;
timer.stop();
}
else if (e.getSource() == resetButton)
{
elapsedTime = 0;
elapsedTimeLabel.setText("0.000 seconds");
timer.stop();
}
else if (running)
{
long currentTime = System.currentTimeMillis();
long elapsed = currentTime - startTime;
elapsedTime += elapsed;
startTime = currentTime;
String elapsedTimeString = String.format("%.3f seconds", elapsedTime / 1000.0);
elapsedTimeLabel.setText(elapsedTimeString);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(StopwatchSwing::new);
}
}