Here's a simple digital clock implemented in Java using the Swing framework. The clock updates every second and displays the current time in hours, minutes, and seconds format.
Code:
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DigitalClock extends JFrame {
private JLabel timeLabel;
public DigitalClock() {
// Setting up the frame
setTitle("Digital Clock");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Adding a label to display time
timeLabel = new JLabel();
timeLabel.setFont(new Font("Arial", Font.PLAIN, 48));
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
// Adding the label to the frame
add(timeLabel);
// Timer to update the clock every second
Timer timer = new Timer(1000, e -> updateClock());
timer.start();
}
// Method to update the clock every second
private void updateClock() {
// Get the current time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());
// Update the label with the new time
timeLabel.setText(currentTime);
}
public static void main(String[] args) {
// Create and show the digital clock
SwingUtilities.invokeLater(() -> {
DigitalClock clock = new DigitalClock();
clock.setVisible(true);
});
}
}
Explanation:
Once you run the program, it will show a window with the current time updating every second.
Looking for more job opportunities? Look no further! Our platform offers a diverse array of job listings across various industries, from technology to healthcare, marketing to finance. Whether you're a seasoned professional or just starting your career journey, you'll find exciting opportunities that match your skills and interests. Explore our platform today and take the next step towards your dream job!
Looking for insightful and engaging blogs packed with related information? Your search ends here! Dive into our collection of blogs covering a wide range of topics, from technology trends to lifestyle tips, finance advice to health hacks. Whether you're seeking expert advice, industry insights, or just some inspiration, our blog platform has something for everyone. Explore now and enrich your knowledge with our informative content!