07 February 2011

Digital clock by using timer (swing) in Java



Here is a piece of code to build a Java clock, using swing,


        Timer t = new Timer(1000, updateClockAction);
        t.start();



ActionListener updateClockAction = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
      // Assumes clock is a custom component
     lblTime.setText(System.currentTimeMillis()+"");
      // OR
    
      // Assumes clock is a JLabel
      lblTime.setText(new Date().toString());
    }
};




 





Here is a piece of code to build a Java clock, using swing,


        Timer t = new Timer(1000, updateClockAction);
        t.start();



ActionListener updateClockAction = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
      // Assumes clock is a custom component
     lblTime.setText(System.currentTimeMillis()+"");
      // OR
    
      // Assumes clock is a JLabel
      lblTime.setText(new Date().toString());
    }
};




 



No comments:

Post a Comment

Thank you.