IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Comment afficher un chronomètre et un compte à rebours dans Swing/Matisse ?


Sujet :

AWT/Swing Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut Comment afficher un chronomètre et un compte à rebours dans Swing/Matisse ?
    Bonjour,

    Je souhaite créer une JFrame dans laquelle il y aurait un chronometre, et surtout un compte à rebours.

    Ex: un compte à rebour de 30 min et un chrono en simultané qui donne le temps restant (ben oui , y'en a qui savent pas compter ....), un bouton start and Stop et un bouton Raz.
    Format 00:00 (minute:seconde).

    Donc je sais déjà faire les boutons et leur associer des actions.


    Mon problème est : quelle action associée ???
    la fonction timer ???
    Comment l'utiliser ???

    Merci pour vos idées .....

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Faudra probablement en faire un bean et fournir un beaninfo (http://java.sun.com/j2se/1.4.2/docs/.../BeanInfo.html). Me semble que c'est comme ca que fait SwingX (on peut rajouter des objets SwingX dans la palette de NetBeans).
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Oki, merci, mais :

    C'est quoi un bean ???
    Y aurait - il quelque part des tutos traitant ce sujet ?

    Merci

  4. #4
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    J'ai plutôt l'impression que Bouye a été égaré par ton titre. Grosso modo netbeans n'a absolument rien à voir la dedans.

    Pour apprendre à utiliser un timer: http://java.sun.com/docs/books/tutor...isc/timer.html

    Il te reste juste à mettre à jour des JLabel contenant le temps parcouru depuis le départ (donc comparer le temps initial t obtenu via un System.currentTimeMillis au temps courrant obtenu via la même méthode et retrancher le résultat du temps prévu).
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 840
    Points : 22 854
    Points
    22 854
    Billets dans le blog
    51
    Par défaut
    Ben vi moi j'ai vu Matisse donc j'ai tout de suite pensé "intégration à la palette de composants de NetBeans".

    => C'est quoi un bean ?
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  6. #6
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Merci les gars pour vos réponses, et en effet, l'erreur vient peut etre de moi,
    je developpe sous MyEclipse, et il y à la tablette Matisse de Netbeans intégrée avec Swing.

    Voila,

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Re - Bonjour...
    Bon après quelques recherches sur le net , j'ai un semblant de début d'application qui tourne ...sauf que ....
    Mon chrono : start : il part, pas de probleme,
    : stop : il s'arrête pas....et je vois pas pourquoi, le pire c'est que le bouton change bien de start à stop et inversement, mais il s'arrête pas et quand je reclique sur start, le "délais", double.....bref, je crois que je suis dans une boucle.....

    Donc, je vous joins le code complet, si quelqu'un peut m'aider....merci !!!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
     
    /*
     * TableDeMarques.java
     *
     * Created on __DATE__, __TIME__
     */
     
    package main;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.Timer;
     
    /**
     * 
     * @author __USER__ */
    public class TableDeMarques extends javax.swing.JFrame {
    	private static int minute = 20, seconde = 00;
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	/** Creates new form TableDeMarques */
    	public TableDeMarques() {
    		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.
             */
    	// GEN-BEGIN:initComponents
    	// <editor-fold defaultstate="collapsed" desc="Generated Code">
    	private void initComponents() {
     
    		jPanel1 = new javax.swing.JPanel();
    		jPanel2 = new javax.swing.JPanel();
    		jButton1 = new javax.swing.JButton();
    		jButton2 = new javax.swing.JButton();
    		jLabel1 = new javax.swing.JLabel();
    		jLabel2 = new javax.swing.JLabel();
    		jPanel3 = new javax.swing.JPanel();
    		menuBar = new javax.swing.JMenuBar();
    		fileMenu = new javax.swing.JMenu();
    		openMenuItem = new javax.swing.JMenuItem();
    		saveMenuItem = new javax.swing.JMenuItem();
    		saveAsMenuItem = new javax.swing.JMenuItem();
    		exitMenuItem = new javax.swing.JMenuItem();
    		editMenu = new javax.swing.JMenu();
    		cutMenuItem = new javax.swing.JMenuItem();
    		copyMenuItem = new javax.swing.JMenuItem();
    		pasteMenuItem = new javax.swing.JMenuItem();
    		deleteMenuItem = new javax.swing.JMenuItem();
    		helpMenu = new javax.swing.JMenu();
    		contentsMenuItem = new javax.swing.JMenuItem();
    		aboutMenuItem = new javax.swing.JMenuItem();
     
    		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    		setTitle("Table De Marques");
    		setMinimumSize(new java.awt.Dimension(800, 800));
    		setName("Table De Marques");
     
    		jPanel1.setLayout(new java.awt.BorderLayout());
     
    		jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(
    				153, 0, 51), 2, true));
     
    		jButton1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
    		jButton1.setText("Start");
    		jButton1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				int delais = 1000;
    				ActionListener tache_timer;
     
    				/* Action réalisé par le timer */
    				tache_timer = new ActionListener() {
    					public void actionPerformed(ActionEvent e1) {
    						if (seconde == 00) {
    							seconde = 60;
    							minute--;
    						}
    						seconde--;
    						jLabel2.setText(minute + ":" + seconde);/* rafraichir le label */
    					}
    				};
    				/* instanciation du timer */
    			    final Timer timer1 = new Timer(delais, tache_timer);
     
     
    				/* Action provoqué par l'utilisateur */
    				/* Lors du clic sur le bouton Start */
    				String texte;
    				texte = jButton1.getText();
    				if (texte.compareTo("Start") == 0) {
    					jButton1.setText("Stop ");
    					timer1.start();
    				} else if (texte.compareTo("Stop ") == 0) {
    					jButton1.setText("Start");
    					timer1.stop();
    				}
    			}
     
    		});
     
    		jButton2.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
    		jButton2.setText("RAZ");
    		jButton2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				/* Lors du clic sur le bouton RAZ */
    				String texte;
    				texte = jButton1.getText();
    				if (texte.compareTo("Start") == 0) {
     
    					minute = 20;
    					seconde = 00;
    					jButton1.setText("Start");
    					jLabel2.setText(minute + ":" + seconde);
    				}
    			}
    		});
     
    		jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
    		jLabel1.setText("Time :");
     
    		jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
    		jLabel2.setText(minute + ":" + seconde);
     
    		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
    				jPanel2);
    		jPanel2.setLayout(jPanel2Layout);
    		jPanel2Layout
    				.setHorizontalGroup(jPanel2Layout
    						.createParallelGroup(
    								javax.swing.GroupLayout.Alignment.LEADING)
    						.addGroup(
    								jPanel2Layout
    										.createSequentialGroup()
    										.addGap(103, 103, 103)
    										.addGroup(
    												jPanel2Layout
    														.createParallelGroup(
    																javax.swing.GroupLayout.Alignment.LEADING)
    														.addGroup(
    																jPanel2Layout
    																		.createSequentialGroup()
    																		.addComponent(
    																				jLabel1,
    																				javax.swing.GroupLayout.PREFERRED_SIZE,
    																				63,
    																				javax.swing.GroupLayout.PREFERRED_SIZE)
    																		.addPreferredGap(
    																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    																		.addComponent(
    																				jLabel2,
    																				javax.swing.GroupLayout.PREFERRED_SIZE,
    																				84,
    																				javax.swing.GroupLayout.PREFERRED_SIZE))
    														.addGroup(
    																jPanel2Layout
    																		.createSequentialGroup()
    																		.addComponent(
    																				jButton1,
    																				javax.swing.GroupLayout.PREFERRED_SIZE,
    																				92,
    																				javax.swing.GroupLayout.PREFERRED_SIZE)
    																		.addPreferredGap(
    																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    																		.addComponent(
    																				jButton2)))
    										.addContainerGap(152, Short.MAX_VALUE)));
    		jPanel2Layout
    				.setVerticalGroup(jPanel2Layout
    						.createParallelGroup(
    								javax.swing.GroupLayout.Alignment.LEADING)
    						.addGroup(
    								jPanel2Layout
    										.createSequentialGroup()
    										.addGap(9, 9, 9)
    										.addGroup(
    												jPanel2Layout
    														.createParallelGroup(
    																javax.swing.GroupLayout.Alignment.BASELINE)
    														.addComponent(jLabel1)
    														.addComponent(jLabel2))
    										.addPreferredGap(
    												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    										.addGroup(
    												jPanel2Layout
    														.createParallelGroup(
    																javax.swing.GroupLayout.Alignment.BASELINE)
    														.addComponent(jButton2)
    														.addComponent(jButton1))
    										.addGap(36, 36, 36)));
     
    		jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);
     
    		jPanel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(
    				0, 102, 0), 2, true));
    		jPanel3.setLayout(new java.awt.BorderLayout());
    		jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);
     
    		fileMenu.setText("File");
     
    		openMenuItem.setText("Open");
    		fileMenu.add(openMenuItem);
     
    		saveMenuItem.setText("Save");
    		fileMenu.add(saveMenuItem);
     
    		saveAsMenuItem.setText("Save As ...");
    		fileMenu.add(saveAsMenuItem);
     
    		exitMenuItem.setText("Exit");
    		exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent evt) {
    				exitMenuItemActionPerformed(evt);
    			}
    		});
    		fileMenu.add(exitMenuItem);
     
    		menuBar.add(fileMenu);
     
    		editMenu.setText("Edit");
     
    		cutMenuItem.setText("Cut");
    		editMenu.add(cutMenuItem);
     
    		copyMenuItem.setText("Copy");
    		editMenu.add(copyMenuItem);
     
    		pasteMenuItem.setText("Paste");
    		editMenu.add(pasteMenuItem);
     
    		deleteMenuItem.setText("Delete");
    		editMenu.add(deleteMenuItem);
     
    		menuBar.add(editMenu);
     
    		helpMenu.setText("Help");
     
    		contentsMenuItem.setText("Contents");
    		helpMenu.add(contentsMenuItem);
     
    		aboutMenuItem.setText("About");
    		helpMenu.add(aboutMenuItem);
     
    		menuBar.add(helpMenu);
     
    		setJMenuBar(menuBar);
     
    		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
    				getContentPane());
    		getContentPane().setLayout(layout);
    		layout.setHorizontalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
    				jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
    				javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE));
    		layout.setVerticalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
    				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 279,
    				Short.MAX_VALUE));
     
    		pack();
    	}
     
    	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
    		System.exit(0);
    	}// GEN-LAST:event_exitMenuItemActionPerformed
     
    	/**
             * @param args
             *            the command line arguments
             */
    	public static void main(String args[]) {
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				new TableDeMarques().setVisible(true);
    			}
     
    		});
    	}
     
    	// GEN-BEGIN:variables
    	// Variables declaration - do not modify
    	private javax.swing.JMenuItem aboutMenuItem;
    	private javax.swing.JMenuItem contentsMenuItem;
    	private javax.swing.JMenuItem copyMenuItem;
    	private javax.swing.JMenuItem cutMenuItem;
    	private javax.swing.JMenuItem deleteMenuItem;
    	private javax.swing.JMenu editMenu;
    	private javax.swing.JMenuItem exitMenuItem;
    	private javax.swing.JMenu fileMenu;
    	private javax.swing.JMenu helpMenu;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JLabel jLabel1;
    	private javax.swing.JLabel jLabel2;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JPanel jPanel3;
    	private javax.swing.JMenuBar menuBar;
    	private javax.swing.JMenuItem openMenuItem;
    	private javax.swing.JMenuItem pasteMenuItem;
    	private javax.swing.JMenuItem saveAsMenuItem;
    	private javax.swing.JMenuItem saveMenuItem;
    	// End of variables declaration//GEN-END:variables
     
    }

  8. #8
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Tu devrais plutôt décolarer ton timer en tant qu'attribut privé de ta classe et uniquement faire des start/stop dans les listeners. En effet là c'est totalement le bordel ton truc, à savoir que tu crées un nouveau timer à chaque action et que tu ne le stopes pas forcément.
    Tu as juste besoin d'un seul timer sur lequel tu fais du start et du stop.

    Genre

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.Timer;
     
    public class MaClasse {
        private Timer t;
        private JButton b;
     
        public MaClasse() {
            t = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    doSomething();
                }
            });
            b = new JButton("Start");
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!t.isRunning()) {
                        t.start();
                        b.setText("Stop");
                    } else {
                        t.stop();
                        b.setText("Start");
                    }
                }
            });
        }
     
        private void doSomething() {
            //Mettre à jour les labels ici
            // ...
        }
     
    }
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  9. #9
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Merci pour la réponse,
    mais je dois faire une classe a part , ou je l'inclus dedans ???
    Pourrais -tu me l'inclure dans mon code, enfin,, me dire a quel endroit je dois le mettre ?

    Merci,

  10. #10
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Bah tu transposes, c'est pas la mort à faire.


    Pour commencer tu vires tout ce que tu as fait dans initComponents. Cette méthode est à réserver uniquement à Netbeans. Elle ne contiendra que ce qui concerne le placement et l'aspect graphique des composants, et surtout rien d'autre. De fait le développeur NE DOIT PAS modifier cette partie.

    Je cite expressément:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    /**
             * 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.
             */
    	// GEN-BEGIN:initComponents
    	// <editor-fold defaultstate="collapsed" desc="Generated Code"

    Puis gentiment tu crées ta variable Timer en tant qu'attribut de ta classe comme je l'ai fait(et donc bye bye le final Timer tout crade posé n'importe où qui est la source de ton problème).

    Puis dans le constructeur de ta classe tu crées ce timer avec le listener qui va bien (donc tache_timer dans ton cas, accessoirement tu ferais bien d'aller lire un tant soit peu les conventions de codage en java).

    Puis toujours dans ton éditeur netbeans tu ajoutes un ActionListener à ton JButton via l'interface de design de netbeans. Celà va te créer une méthode privée dans ta classe du nom de jButton1ActionPertformed ou quelque chose dans ce style. Dans cette méthode tu ajouteras le code de démarrage/arrêt du timer.

    Voila
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  11. #11
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Salut Sinok....

    Merci pour ta réponse,
    j'ai donc essayé ce que tu m'as dit , mais j'ai dû faire un couac quelquepart parce que ca fonctionne toujours pô.....
    Tu verras le nouveau code,
    le chrono ne s'affiche pas et le bouton start ne change pas...en bref, je dois oublier de lancer quelquechose ....non ??

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    /*
     * TableDeMarques.java
     *
     * Created on __DATE__, __TIME__
     */
     
    package main;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.Timer;
     
    /**
     *
     * @author  __USER__
     */
    public class TableDeMarques extends javax.swing.JFrame {
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
     
    	/** Creates new form TableDeMarques */
    	public TableDeMarques() {
    		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.
             */
    	//GEN-BEGIN:initComponents
    	// <editor-fold defaultstate="collapsed" desc="Generated Code">
    	private void initComponents() {
     
    		jPanel2 = new javax.swing.JPanel();
    		jPanel1 = new javax.swing.JPanel();
    		jLabel1 = new javax.swing.JLabel();
    		jLabel2 = new javax.swing.JLabel();
    		jButton1 = new javax.swing.JButton();
    		jButton2 = new javax.swing.JButton();
    		menuBar = new javax.swing.JMenuBar();
    		fileMenu = new javax.swing.JMenu();
    		openMenuItem = new javax.swing.JMenuItem();
    		saveMenuItem = new javax.swing.JMenuItem();
    		saveAsMenuItem = new javax.swing.JMenuItem();
    		exitMenuItem = new javax.swing.JMenuItem();
    		editMenu = new javax.swing.JMenu();
    		cutMenuItem = new javax.swing.JMenuItem();
    		copyMenuItem = new javax.swing.JMenuItem();
    		pasteMenuItem = new javax.swing.JMenuItem();
    		deleteMenuItem = new javax.swing.JMenuItem();
    		helpMenu = new javax.swing.JMenu();
    		contentsMenuItem = new javax.swing.JMenuItem();
    		aboutMenuItem = new javax.swing.JMenuItem();
     
    		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
    				jPanel2);
    		jPanel2.setLayout(jPanel2Layout);
    		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100,
    				Short.MAX_VALUE));
    		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100,
    				Short.MAX_VALUE));
     
    		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
    		jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
     
    		jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 18));
    		jLabel1.setText("Time :");
    		jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(
    				570, 0, -1, -1));
     
    		jLabel2.setFont(new java.awt.Font("Segoe UI", 1, 24));
    		jLabel2.setText("jLabel2");
    		jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(
    				630, 0, -1, -1));
     
    		jButton1.setFont(new java.awt.Font("Segoe UI", 1, 18));
    		jButton1.setText("Start");
    		jPanel1.add(jButton1,
    				new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 30, -1,
    						-1));
     
    		jButton2.setFont(new java.awt.Font("Segoe UI", 0, 18));
    		jButton2.setText("RAZ");
    		jPanel1.add(jButton2,
    				new org.netbeans.lib.awtextra.AbsoluteConstraints(650, 30, -1,
    						-1));
     
    		fileMenu.setText("File");
     
    		openMenuItem.setText("Open");
    		fileMenu.add(openMenuItem);
     
    		saveMenuItem.setText("Save");
    		fileMenu.add(saveMenuItem);
     
    		saveAsMenuItem.setText("Save As ...");
    		fileMenu.add(saveAsMenuItem);
     
    		exitMenuItem.setText("Exit");
    		exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
    			public void actionPerformed(java.awt.event.ActionEvent evt) {
    				exitMenuItemActionPerformed(evt);
    			}
    		});
    		fileMenu.add(exitMenuItem);
     
    		menuBar.add(fileMenu);
     
    		editMenu.setText("Edit");
     
    		cutMenuItem.setText("Cut");
    		editMenu.add(cutMenuItem);
     
    		copyMenuItem.setText("Copy");
    		editMenu.add(copyMenuItem);
     
    		pasteMenuItem.setText("Paste");
    		editMenu.add(pasteMenuItem);
     
    		deleteMenuItem.setText("Delete");
    		editMenu.add(deleteMenuItem);
     
    		menuBar.add(editMenu);
     
    		helpMenu.setText("Help");
     
    		contentsMenuItem.setText("Contents");
    		helpMenu.add(contentsMenuItem);
     
    		aboutMenuItem.setText("About");
    		helpMenu.add(aboutMenuItem);
     
    		menuBar.add(helpMenu);
     
    		setJMenuBar(menuBar);
     
    		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
    				getContentPane());
    		getContentPane().setLayout(layout);
    		layout.setHorizontalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
    				jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
    				javax.swing.GroupLayout.DEFAULT_SIZE, 1160, Short.MAX_VALUE));
    		layout.setVerticalGroup(layout.createParallelGroup(
    				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
    				layout.createSequentialGroup().addComponent(jPanel1,
    						javax.swing.GroupLayout.PREFERRED_SIZE, 68,
    						javax.swing.GroupLayout.PREFERRED_SIZE)
    						.addContainerGap(575, Short.MAX_VALUE)));
     
    		pack();
    	}// </editor-fold>
    	//GEN-END:initComponents
     
    	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
    		System.exit(0);
    	}//GEN-LAST:event_exitMenuItemActionPerformed
     
    	/**
             * @param args the command line arguments
             */
    	public static void main(String args[]) {
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				new TableDeMarques().setVisible(true);
    			}
    		});
    	}
     
    	public class Chrono {
    	    private Timer t;
     
    	    public Chrono() {
    	        t = new Timer(1000, new ActionListener() {
    	            public void actionPerformed(ActionEvent e) {
    	            	 doSomething();
    	            }
    	        });
    	        jButton1 = new JButton("Start");
    	        jButton1.addActionListener(new ActionListener() {
    	            public void actionPerformed(ActionEvent e) {
    	                if (!t.isRunning()) {
    	                    t.start();
    	                    jButton1.setText("Stop");
    	                } else {
    	                    t.stop();
    	                  jButton1.setText("Start");
    	                }
    	            }
    	        });
     
     
    	    }
    	    private void doSomething() {
    	    	int seconde = 00;
    			int minute = 20;
    			if (seconde == 00) {
    				seconde = 60;
    				minute--;
    			}
    			seconde--;
    			jLabel2.setText(minute + ":" + seconde);/* rafraichir le label */
     
            }
    	};
     
    	//GEN-BEGIN:variables
    	// Variables declaration - do not modify
    	private javax.swing.JMenuItem aboutMenuItem;
    	private javax.swing.JMenuItem contentsMenuItem;
    	private javax.swing.JMenuItem copyMenuItem;
    	private javax.swing.JMenuItem cutMenuItem;
    	private javax.swing.JMenuItem deleteMenuItem;
    	private javax.swing.JMenu editMenu;
    	private javax.swing.JMenuItem exitMenuItem;
    	private javax.swing.JMenu fileMenu;
    	private javax.swing.JMenu helpMenu;
    	private javax.swing.JButton jButton1;
    	private javax.swing.JButton jButton2;
    	private javax.swing.JLabel jLabel1;
    	private javax.swing.JLabel jLabel2;
    	private javax.swing.JPanel jPanel1;
    	private javax.swing.JPanel jPanel2;
    	private javax.swing.JMenuBar menuBar;
    	private javax.swing.JMenuItem openMenuItem;
    	private javax.swing.JMenuItem pasteMenuItem;
    	private javax.swing.JMenuItem saveAsMenuItem;
    	private javax.swing.JMenuItem saveMenuItem;
    	// End of variables declaration//GEN-END:variables
     
    }

  12. #12
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    tu ne dois pas mettre le timer dans une classe interne mais directement dans ta classe.

    De fait le contenu de ta classe chrono devrait se trouver dans ta classe TableDeMarques.

    Bref, t'as pas mal à relire dans tes cours sur le java en lui même avant de t'attaquer à la partie graphique. J'ai l'impression que tu n'entartes pas grand chose là.
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  13. #13
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    En effet, je suis un gros boulet sur Swing...bref, bon j'essaye ca et je te dis

    ....

  14. #14
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Bon, ben c'est cool , ca fonctionne impeccable, voir même deux peccable...

    Merci Sinok, tu passeras le bonjour a choco ???

    J'ai suivi ton conseil et j'ai relus les standards.....

  15. #15
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    Bon, le timer marche , mais je mes suis rendu compte qu'il ne se réinitialize pas....
    j'ai un bouton "raz", qui remet le chrono a 20:00, mais il repart au dernier temps arrété....

    Ca va c'est clair ???

    Si quelqu'un a une idée......j'ai essayé avec un Restart()....
    mais rien ....


    Merci d'avance...

  16. #16
    Futur Membre du Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 29
    Points : 7
    Points
    7
    Par défaut
    C'est bon ....j'ai trouvé....

    Merci......

  17. #17
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2014
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mars 2014
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bonsoir,
    j'ai le même souci que ceci je veux utiliser un chronomètre dans mon application java Swing
    je travaille sous netBeans et j'ai copié le code concernant le timer mais ça marche pas quelqu'un veux bien venir en aide

    merci d'avance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment afficher le contenu d'un fichier ini dans un mémo?
    Par Jayceblaster dans le forum Langage
    Réponses: 7
    Dernier message: 13/05/2014, 17h38
  2. Afficher heure + compte à rebours dans barre de titre
    Par Zaza91280 dans le forum VB.NET
    Réponses: 2
    Dernier message: 14/04/2010, 15h23
  3. Réponses: 5
    Dernier message: 04/04/2008, 20h14
  4. Réponses: 13
    Dernier message: 26/07/2007, 08h50
  5. Réponses: 7
    Dernier message: 31/10/2006, 00h36

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo