import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CounterApp extends JFrame {
private int count = 0;
private boolean running = false;
private Timer timer;
private JLabel countLabel;
public CounterApp() {
setTitle("Counter App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLayout(new BorderLayout());
countLabel = new JLabel("Count: " + count);
countLabel.setHorizontalAlignment(JLabel.CENTER);
countLabel.setFont(new Font("Arial", Font.PLAIN, 24));
add(countLabel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton runButton = new JButton("Run");
JButton pauseButton = new JButton("Pause");
JButton resetButton = new JButton("Reset");
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startCounter();
}
});
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseCounter();
}
});
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetCounter();
}
});
buttonPanel.add(runButton);
buttonPanel.add(pauseButton);
buttonPanel.add(resetButton);
add(buttonPanel, BorderLayout.SOUTH);
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (running) {
count++;
countLabel.setText("Count: " + count);
}
}
});
}
private void startCounter() {
running = true;
timer.start();
}
private void pauseCounter() {
running = false;
timer.stop();
}
private void resetCounter() {
running = false;
count = 0;
countLabel.setText("Count: " + count);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CounterApp app = new CounterApp();
app.setVisible(true);
}
});
}
}