adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

How To Create Digital Clock in java

 How To Create Digital Clock in java




import javHow To Create Digital Clock in javaa.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.awt.Color;

public class DigitalClock extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DigitalClock frame = new DigitalClock();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * Create the frame.
	 */
	public DigitalClock() {   
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 608, 391);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblTest = new JLabel("Test");
		lblTest.setForeground(Color.RED);
		lblTest.setFont(new Font("LCDMono2", Font.BOLD, 55));
		lblTest.setHorizontalAlignment(SwingConstants.CENTER);
		lblTest.setBounds(133, 126, 337, 97);
		contentPane.add(lblTest);
		
//		Digital clock
		
		Timer timer = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				// Create Date Object
				Date date = new Date();
				System.out.println(date.toString());
				System.out.println(date.toString().substring(11, 20));
				
//				Tue Sep 21 11:09:34 IST 2021
				
				lblTest.setText(date.toString().substring(11, 20));
			}
		});
		
		timer.start();  // timer start
	}

}


Output 

 

Video


Post a Comment

0 Comments