Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.Serializable; import java.lang.*; import java.lang.Object; import javax.swing.*; import javax.swing.border.*; public class H6 extends Panel implements Serializable { // ---- déclaration des variables private int t=0; // ---- Le timer actualise l'affichage toutes les secondes si delay=1000 private int delay=500; // ------------------------ CREATION DES COMPOSANTS ---------------// ----------- déclaration des boutons Button avant = new Button("Forward"); Button arriere = new Button("Backward"); Button stop = new Button("STOP"); Button fin = new Button("Remise à zéro"); // ----------- déclaration des Labels final Label temps = new Label("temps"); Label pasdetemps_Label = new Label("pas de temps"); // ----------- déclaration des zones de texte final TextField pasdetemps = new TextField(4); PropertyChangeSupport changeSupport; public H6() { setLayout(new GridLayout(2,4,5,5)); changeSupport = new PropertyChangeSupport(this); // ---- Ajout des composants aux conteneurs avec formatage add(pasdetemps_Label); add(pasdetemps); add(temps); add(arriere); add(fin); add(stop); add(avant); } public /*static*/ void main(String[] args) { // ---- déclaration des évènements ActionListener tache_timer_avant; ActionListener tache_timer_arriere; // ---- Action réalisé par le timer AVANT tache_timer_avant= new ActionListener() { public void actionPerformed(ActionEvent e) { int pdt = (int)(Double.parseDouble(pasdetemps.getText())); t=t+pdt; temps.setText("temps : "+t); } }; // ---- instanciation du timer final Timer timer_avant= new Timer(delay,tache_timer_avant); // ---- Action réalisé par le timer ARRIERE tache_timer_arriere= new ActionListener() { public void actionPerformed(ActionEvent e) { int pdt = (int)(Double.parseDouble(pasdetemps.getText())); t=t-pdt; temps.setText("temps : "+t); } }; // ---- instanciation du timer final Timer timer_arriere= new Timer(delay,tache_timer_arriere); // ---- Action provoqué par l'utilisateur lors du clic sur le bouton AVANT avant.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e_av) { timer_avant.start(); timer_arriere.stop(); } }); // ---- Action provoqué par l'utilisateur lors du clic sur le bouton ARRIERE arriere.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { timer_avant.stop(); timer_arriere.start(); } }); // ---- Action provoqué par l'utilisateur lors du clic sur le bouton FIN fin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { timer_avant.stop(); timer_arriere.stop(); t=0; } }); // ---- Action provoqué par l'utilisateur lors du clic sur le bouton STOP stop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { timer_avant.stop(); timer_arriere.stop(); temps.setText("temps : "+t); } }); } public synchronized int getT() { return t; } public synchronized void setT(int v) { int oldt = t; t = v; changeSupport.firePropertyChange("t", oldt, t); } public synchronized void addPropertyChangeListener (PropertyChangeListener l) { changeSupport.addPropertyChangeListener(l); } public synchronized void removePropertyChangeListener (PropertyChangeListener l) { changeSupport.removePropertyChangeListener(l); } }