Digital Clock
Digital Clock
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public DigitalClock() {
setTitle("Digital Clock");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timeLabel.setForeground(currentColor);
add(timeLabel, BorderLayout.CENTER);
buttonPanel.setLayout(new FlowLayout());
// Button to change color of the clock text
colorButton.addActionListener(new ActionListener() {
@Override
changeColor();
});
formatButton.addActionListener(new ActionListener() {
@Override
toggleTimeFormat();
});
buttonPanel.add(colorButton);
buttonPanel.add(formatButton);
add(buttonPanel, BorderLayout.SOUTH);
setSize(500, 300);
setVisible(true);
// Timer to update the time every second
@Override
updateClock();
});
timer.start();
updateClock();
SimpleDateFormat sdf;
if (is24HourFormat) {
// 24-hour format
} else {
if (currentColor.equals(Color.BLUE)) {
currentColor = Color.GREEN;
} else {
currentColor = Color.BLUE;
timeLabel.setForeground(currentColor);
is24HourFormat = !is24HourFormat;
updateClock();
new DigitalClock();