Affichage des articles dont le libellé est Java Digital Clock In Netbeans. Afficher tous les articles
Affichage des articles dont le libellé est Java Digital Clock In Netbeans. Afficher tous les articles

Java Digital Clock In Netbeans

How to Create Digital Clock in Java NetBeans

Java Digital Clock In Netbeans




In this Java Tutorial we will see How To Make a Digital Clock, With Hours, Minutes and Seconds In Netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.

What We Will Do In This Project:

- Create a JPanel named centerPanel with a visually appealing gradient background.
- Create a JLabel to display the current time.
Update Timer continuously using a Timer that calls the updateClock method every second.





Project Source Code:


package new_tutorials;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;



public class Digital_Clock extends JFrame {

    private JLabel timeLabel;
    
    public Digital_Clock(){
    
        setTitle("Digital Clock");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLocationRelativeTo(null);
        setResizable(false);

        
    // Create a JPanel with a background gradient    
    JPanel centerPanel = new JPanel(new GridBagLayout()){
    
        @Override
        protected void paintComponent(Graphics g){
            
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            
            // Define a gradient paint for the background
            GradientPaint gradient = new GradientPaint(new Point(0,0), new Color(37, 116, 169),
                                new Point(0,getHeight()), new Color(78, 154, 217) );
            
            g2d.setPaint(gradient);
            
            // Fill the panel with the gradient paint
            g2d.fillRect(0,0,getWidth(), getHeight());
            g2d.dispose();          
        }
    };
    add(centerPanel);
    
    timeLabel = new JLabel();
    timeLabel.setFont(new Font("Arial", Font.BOLD, 56));
    timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    timeLabel.setForeground(Color.white);
    
    
     // Add timeLabel to the center panel with padding
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(30, 30, 30, 30);  // Padding
        
        centerPanel.add(timeLabel, gbc);
        
        // Start a timer to update the clock every second
        Timer timer = new Timer(1000, e->updateClock());
        timer.start();
        
        // Initial clock update
        updateClock();
    
    }
    
    private void updateClock(){
        
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String currentTime = sdf .format(new Date());
        timeLabel.setText(currentTime);
        
    }
    
    
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(()->{});
        
           Digital_Clock dc = new Digital_Clock();
           dc.setVisible(true);
        
    }
    
    
}


The Final Result:



Java Digital Clock