0% found this document useful (0 votes)
4 views

Digital Clock

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Digital Clock

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package digitalclock;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.SimpleDateFormat;

import java.util.Date;

public class DigitalClock extends JFrame {

private JLabel timeLabel;

private JButton colorButton;

private JButton formatButton;

private boolean is24HourFormat = true;

private Color currentColor = Color.BLUE;

public DigitalClock() {

// Set up the frame

setTitle("Digital Clock");

setLayout(new BorderLayout());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Time label to display the current time

timeLabel = new JLabel("", SwingConstants.CENTER);

timeLabel.setFont(new Font("Arial", Font.BOLD, 60));

timeLabel.setForeground(currentColor);

add(timeLabel, BorderLayout.CENTER);

// Panel to hold buttons

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new FlowLayout());
// Button to change color of the clock text

colorButton = new JButton("Change Color");

colorButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

changeColor();

});

// Button to toggle time format (12-hour / 24-hour)

formatButton = new JButton("Toggle Format");

formatButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

toggleTimeFormat();

});

// Add buttons to the panel

buttonPanel.add(colorButton);

buttonPanel.add(formatButton);

add(buttonPanel, BorderLayout.SOUTH);

// Set the window size and make it visible

setSize(500, 300);

setVisible(true);
// Timer to update the time every second

Timer timer = new Timer(1000, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

updateClock();

});

timer.start();

// Initialize the clock

updateClock();

// Method to update the time on the label

private void updateClock() {

SimpleDateFormat sdf;

if (is24HourFormat) {

// 24-hour format

sdf = new SimpleDateFormat("HH:mm:ss");

} else {

// 12-hour format with AM/PM

sdf = new SimpleDateFormat("hh:mm:ss a");

String currentTime = sdf.format(new Date());


timeLabel.setText(currentTime);

// Method to change the color of the time text

private void changeColor() {

// Toggle between blue and green

if (currentColor.equals(Color.BLUE)) {

currentColor = Color.GREEN;

} else {

currentColor = Color.BLUE;

timeLabel.setForeground(currentColor);

// Method to toggle between 12-hour and 24-hour formats

private void toggleTimeFormat() {

is24HourFormat = !is24HourFormat;

updateClock();

public static void main(String[] args) {

new DigitalClock();

You might also like