[Composant] Affichage d'un horloge
Bonjour,
j'ai développé une sous-classe d'un composant JLabel qui affiche la date et l'heure actuelle. Débutant, j'ai besoin d'être guidé pour savoir si je m'y prends correctement ou pas. J'aimerais avoir votre avis sur cette classe.
Donnez moi vos remarques et observations sur la classe, le style de programmation...
Comment auriez-vous procéder ?
Quelles méthodes auriez-vous implémenté ?
Quelles propriétés auriez-vous défini ?
Trouvez-vous cette classe interessante ?
etc.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| /*
* Clock.java
*
* Created on 4 septembre 2005, 19:46
*
*/
package tutothreadsun;
import java.lang.Runnable;
import java.awt.Color;
import java.util.Date;
import java.lang.InterruptedException;
import javax.swing.JLabel;
import java.text.DateFormat;
import java.util.Locale;
/**
*
* @author zoax
* @version 1.0
*/
public class Clock extends JLabel implements Runnable {
private volatile Thread clockThread; //Thread
private Date currentDate; //Stocke la date actuelle
private DateFormat formatter; //Formatte la date
private Locale locale;
/** Crée une nouvelle instance de Clock
* @param noparam aucun paramètre
* @return noreturn aucune valeur de retour
* @exception noexception aucune exception
*/
public Clock() {
setSize(275,25); //Définit la taille du composant.
currentDate=new Date(); //Initialisation de la date actuelle
locale=Locale.getDefault();
formatter=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.MEDIUM, locale); //Formattage
setText(formatter.format(currentDate)); //Initialisation du texte du JLabel
clockThread=new Thread(this, "Clock"); //Déclaration du Thread
clockThread.start(); //Lancement du Thread
}
/**Lancement du Thread
* @param noparam aucun paramètre
* @return noreturn aucune valeur de retour
* @exception noexception aucune exception
*/
public void run(){
Thread myThread=Thread.currentThread();
while (clockThread==myThread){
updateClock();
try {
Thread.sleep(1000);
}
catch (InterruptedException e){}
}
}
/**Mise à jour de la date
* @param noparam aucun paramètre
* @return noreturn aucune valeur de retour
* @exception noexception aucune exception
*/
private void updateClock() {
String today; //date du jour de type String
currentDate=new Date(); //Date et heure actuelle
today=formatter.format(currentDate); //formatte de la date et de l'heure actuelle
setText(formatter.format(currentDate)); //Mise à jour du texte du JLabel
currentDate=null;
}
public static void main(String[] args){
Clock appli=new Clock();
}
} |
Merci d'avance pour toutes vos remarques.
Ca va bien m'aider.
Ciao.