Complete StopWatch in Java with source code

Complete StopWatch in Java with source code :

















 
 
(Screen Shot)



****************************************
Code Start from Here:

package stopwatch;
/**
 *
 * @author Md. Khafi Hossain
 */
public class Main extends javax.swing.JFrame { 
 
static int milliseconds = 0;
static int seconds = 0;
static int minutes = 0;
static int hours = 0;
static boolean state = true;
    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                       
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("StopWatch");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("00:00:00:000");

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Stop");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Reset");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Lap");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel2.setText("00:00:00:000");

        jButton5.setText("Lap");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel3.setText("00:00:00:000");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4)
                            .addComponent(jButton5))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))))
                .addContainerGap(116, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jLabel3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                     

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
      jButton4.setVisible(true);
     
        state = true;
     Thread time = new Thread(){
         public  void run() {
         
                for (;;) {
                 if (state == true){
                 
            
                try {
                    sleep(1);
                    if (milliseconds>=999){
                        milliseconds = 0;
                        seconds ++;
                    }
                    if (seconds>=59){
                        seconds = 0;
                        minutes ++;
                    }
                    if (minutes>=59){
                        minutes = 0;
                        hours ++;
                    }
                   milliseconds++;
                   jLabel1.setText(hours+":"+ minutes+":"+seconds+":"+milliseconds);
                } catch (Exception e) {
                }
            }
                else{
                      break;
                        }
            }
         }
     };
     time.start();
    }                                     

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                      
   state = false;        // TODO add your handling code here:
    }                                     

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                      
state = false;
jLabel1.setText("00:00:00:000");
hours = 0;
minutes = 0;
seconds = 0;
milliseconds = 0;
jLabel2.setText("00:00:00:000");
jLabel3.setText("00:00:00:000");
jButton4.setVisible(false);
jButton5.setVisible(false);
jLabel2.setVisible(false);
jLabel3.setVisible(false);
jButton4.setEnabled(true);
jButton5.setEnabled(true);
    }                                     

    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                  
      jButton4.setVisible(false);
      jButton5.setVisible(false);
      jLabel2.setVisible(false);
      jLabel3.setVisible(false);
    }                                 

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                      
   jLabel2.setVisible(true);
   jLabel2.setText(jLabel1.getText());
   jButton4.setEnabled(false);
   jButton5.setVisible(true);
    }                                     

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                      
       jLabel3.setVisible(true);
   jLabel3.setText(jLabel1.getText());
   jButton5.setEnabled(false);
 
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                  
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration                
}
***************** End Project ***********************

Comments

  1. works but:
    1, String is not displayed, only the counted time. The timer should start at 00:00:00 and after starting it is 0: 0: 0
    2, Time counts up too slowly. The second is not equal to the real second.
    As for the strings, I advised myself but I have no idea why the counted seconds (time) flows slower. Any suggestions?
    Regards, Bart

    ReplyDelete

Post a Comment

Popular posts from this blog

Complete Python Database Project with sqlite3

System sound recorder using NAudio dll with complete code