J'essaye de créer une application, et j'aimerais ajouter une horloge. Je fais l'horloge en utilisant un JPanel et un ActionListener, et voudrais également utiliser un temporisateur. Le tutoriel Swing dit que pour instancier un Timer, vous diriez un nouveau Timer (numMillis, this (un ActionListener)), cependant, "this" ne semble pas fonctionner avec les éléments JPanel. Que dois-je ajouter au constructeur Timer pour instancier correctement le Timer?Minuterie dans JPanel
public ClockPanel() {
super();
clockLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
clockLabel.setFont(new Font("Monospaced", Font.BOLD, 100));
clockLabel.setOpaque(true);
clockLabel.setBackground(Color.black);
clockLabel.setForeground(Color.white);
timer = new Timer(500, this);
timer.setRepeats(true);
timer.start();
clockLabel.setVisible(true);
initComponents();
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(timer))
clockLabel.setText(sdf.format(new Date(System.currentTimeMillis())));
}
La classe imbriquée ou la classe anonyme est en effet la voie à suivre – Robin