begin process at 2010 02 10 05:28:11
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Swing

 > FAIRE CLIGNOTER UN BOUTON AVEC UN TIMER SWING

FAIRE CLIGNOTER UN BOUTON AVEC UN TIMER SWING


 Information sur la source



 Description

Dans la catégorie sassèrarien, ce programme fait clignoter un bouton...

Source

  • import java.awt.Color;
  • import java.awt.FlowLayout;
  • import java.awt.event.ActionEvent;
  • import java.awt.event.ActionListener;
  • import javax.swing.JButton;
  • import javax.swing.JFrame;
  • import javax.swing.JPanel;
  • import javax.swing.SwingUtilities;
  • import javax.swing.Timer;
  • public class ButtonBlinker extends JFrame {
  • private static final long serialVersionUID = 1L;
  • private JPanel jContentPane = null;
  • private JButton startButton = null;
  • private JButton pauseButton = null;
  • private JButton stopButton = null;
  • private static int blinkInterval = 200; // milliseconds
  • private enum Status { STOPPED, RUNNING, PAUSED };
  • private Status status = Status.STOPPED;
  • private boolean blink = false;
  • private Color pauseColor;
  • private Timer pauseTimer;
  • /**
  • * This is the default constructor
  • */
  • public ButtonBlinker(){
  • super();
  • initialize();
  • }
  • /**
  • * This method initializes this
  • *
  • * @return void
  • */
  • private void initialize(){
  • this.setSize(291, 103);
  • this.setTitle("Button Blinker");
  • this.setContentPane(getJContentPane());
  • pauseColor = pauseButton.getForeground();
  • updateCommandButtons(status = Status.STOPPED);
  • }
  • /**
  • * This method initializes jContentPane
  • *
  • * @return javax.swing.JPanel
  • */
  • private JPanel getJContentPane(){
  • if(jContentPane==null){
  • jContentPane=new JPanel();
  • jContentPane.setLayout(new FlowLayout());
  • jContentPane.add(getStartButton(), null);
  • jContentPane.add(getPauseButton(), null);
  • jContentPane.add(getStopButton(), null);
  • }
  • return jContentPane;
  • }
  • /**
  • * This method initializes startButton
  • *
  • * @return javax.swing.JButton
  • */
  • private JButton getStartButton(){
  • if(startButton==null){
  • startButton=new JButton();
  • startButton.setText("start");
  • startButton.addActionListener(new java.awt.event.ActionListener(){
  • public void actionPerformed(java.awt.event.ActionEvent e){
  • startButtonActionPerformed(e);
  • }
  • });
  • }
  • return startButton;
  • }
  • /**
  • * This method initializes pauseButton
  • *
  • * @return javax.swing.JButton
  • */
  • private JButton getPauseButton(){
  • if(pauseButton==null){
  • pauseButton=new JButton();
  • pauseButton.setText("pause");
  • pauseButton.addActionListener(new java.awt.event.ActionListener(){
  • public void actionPerformed(java.awt.event.ActionEvent e){
  • pauseButtonActionPerformed(e);
  • }
  • });
  • }
  • return pauseButton;
  • }
  • /**
  • * This method initializes stopButton
  • *
  • * @return javax.swing.JButton
  • */
  • private JButton getStopButton(){
  • if(stopButton==null){
  • stopButton=new JButton();
  • stopButton.setText("stop");
  • stopButton.addActionListener(new java.awt.event.ActionListener(){
  • public void actionPerformed(java.awt.event.ActionEvent e){
  • stopButtonActionPerformed(e);
  • }
  • });
  • }
  • return stopButton;
  • }
  • private void stopTimer(Timer timer) {
  • if (timer != null) timer.stop();
  • timer = null;
  • }
  • private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
  • updateCommandButtons(status = Status.RUNNING);
  • }
  • private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {
  • updateCommandButtons(switchSuspended());
  • // setup or stop blinker
  • if (status == Status.PAUSED) {
  • (pauseTimer = new Timer(blinkInterval, new ActionListener() {
  • public void actionPerformed(ActionEvent evt) {
  • pauseButton.setForeground((blink = !blink) ? pauseColor : Color.RED);
  • }})).start();
  • } else {
  • stopTimer(pauseTimer);
  • }
  • }
  • private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
  • updateCommandButtons(status = Status.STOPPED);
  • stopTimer(pauseTimer);
  • }
  • private Status switchSuspended() { return status = (status == Status.RUNNING ? Status.PAUSED : Status.RUNNING); }
  • public void updateCommandButtons(Status status) {
  • switch (status) {
  • case RUNNING:
  • startButton.setEnabled(false);
  • pauseButton.setEnabled(true);
  • stopButton.setEnabled(true);
  • pauseButton.setForeground(pauseColor);
  • System.out.println("started");
  • break;
  • case PAUSED:
  • startButton.setEnabled(false);
  • pauseButton.setEnabled(true);
  • stopButton.setEnabled(true);
  • System.out.println("paused");
  • break;
  • case STOPPED:
  • startButton.setEnabled(true);
  • pauseButton.setEnabled(false);
  • stopButton.setEnabled(false);
  • System.out.println("stopped");
  • break;
  • }
  • }
  • public static void main(String[] args){
  • SwingUtilities.invokeLater(new Runnable(){
  • public void run(){
  • ButtonBlinker thisClass=new ButtonBlinker();
  • thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • thisClass.setVisible(true);
  • }
  • });
  • }
  • }
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ButtonBlinker extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton startButton = null;
	private JButton pauseButton = null;
	private JButton stopButton = null;

	private static int blinkInterval = 200; // milliseconds
	private enum Status { STOPPED, RUNNING, PAUSED };
	private Status status = Status.STOPPED;
	private boolean blink = false; 
	private Color pauseColor; 
	private Timer pauseTimer; 
	

	/**
	 * This is the default constructor
	 */
	public ButtonBlinker(){
		super();
		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(){
		this.setSize(291, 103);
		this.setTitle("Button Blinker");
		this.setContentPane(getJContentPane());
		pauseColor = pauseButton.getForeground();
		updateCommandButtons(status = Status.STOPPED);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane(){
		if(jContentPane==null){
			jContentPane=new JPanel();
			jContentPane.setLayout(new FlowLayout());
			jContentPane.add(getStartButton(), null);
			jContentPane.add(getPauseButton(), null);
			jContentPane.add(getStopButton(), null);
		}
		return jContentPane;
	}
	/**
	 * This method initializes startButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getStartButton(){
		if(startButton==null){
			startButton=new JButton();
			startButton.setText("start");
			startButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){
					startButtonActionPerformed(e);
				}
			});
		}
		return startButton;
	}
	/**
	 * This method initializes pauseButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPauseButton(){
		if(pauseButton==null){
			pauseButton=new JButton();
			pauseButton.setText("pause");
			pauseButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){
					pauseButtonActionPerformed(e);
				}
			});
		}
		return pauseButton;
	}
	/**
	 * This method initializes stopButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getStopButton(){
		if(stopButton==null){
			stopButton=new JButton();
			stopButton.setText("stop");
			stopButton.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e){					
					stopButtonActionPerformed(e);
				}
			});
		}
		return stopButton;
	}

	private void stopTimer(Timer timer) { 
		if (timer != null) timer.stop(); 
		timer = null; 
	} 

	private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
		updateCommandButtons(status = Status.RUNNING);
	} 

	private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) { 
		updateCommandButtons(switchSuspended()); 
		// setup or stop blinker 
		if (status == Status.PAUSED) {
			(pauseTimer = new Timer(blinkInterval, new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					pauseButton.setForeground((blink = !blink) ? pauseColor : Color.RED);
				}})).start(); 
		} else { 
			stopTimer(pauseTimer);                  
		} 
	} 

	private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
		updateCommandButtons(status = Status.STOPPED);				
		stopTimer(pauseTimer); 
	} 

	private Status switchSuspended() { return status = (status == Status.RUNNING ? Status.PAUSED : Status.RUNNING); }

	public void updateCommandButtons(Status status) { 
		switch (status) { 
			case RUNNING: 
				startButton.setEnabled(false); 
				pauseButton.setEnabled(true); 
				stopButton.setEnabled(true);
				pauseButton.setForeground(pauseColor);
				System.out.println("started");
				break; 
			case PAUSED: 
				startButton.setEnabled(false); 
				pauseButton.setEnabled(true); 
				stopButton.setEnabled(true);
				System.out.println("paused");
				break; 
			case STOPPED: 
				startButton.setEnabled(true); 
				pauseButton.setEnabled(false); 
				stopButton.setEnabled(false);
				System.out.println("stopped");
				break; 
		} 
	} 

	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				ButtonBlinker thisClass=new ButtonBlinker();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}
} 



 Sources du même auteur

CHUNKEDXML, LIRE DU XML PAR MORCEAU
Source avec Zip EXPLORATEUR D'IMAGES
Source avec Zip ANALYSEUR XML GÉNÉRIQUE ET PROGRAMMATION REFLÉXIVE
Source avec Zip REDIRECTION D'URL ET TRANSMISSION DE COOKIES
SUPPRIMER LES BALISES D'UN FICHIER HTML

 Sources de la même categorie

JLABELIMAGE : GESTION D'UNE IMAGE EN ARRIÈRE-PLAN par bob22mael
Source avec Zip SÉLECTEUR DE DATES MULTIPLES, ORIGINE UHRAND par William44290
RECHERCHE D'ELEMENTS DANS UNE JLIST par Ze_Hulk
Source avec Zip Source avec une capture PROGICIEL DE LOCATION DE VOITURE par SoftDeath
Source avec Zip Source avec une capture CALENDRIER ET HORLOGE AVEC LA LIBRAIRIE ORG.JDESKTOP.SWINGX par Cornellus1985

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture JIDBASE : JAVA INTERFACE DATABASE par sovos
Source avec Zip Source avec une capture UN JEU COMPLET EN JPA par ulm950
RECHERCHE D'ELEMENTS DANS UNE JLIST par Ze_Hulk
Source avec Zip Source avec une capture PROGICIEL DE LOCATION DE VOITURE par SoftDeath
Source avec Zip Source avec une capture CALENDRIER ET HORLOGE AVEC LA LIBRAIRIE ORG.JDESKTOP.SWINGX par Cornellus1985

Commentaires et avis

Commentaire de danimo le 18/07/2007 06:58:23

Bonjour,


Mais ou sont passees les classes enum et Status ?

Commentaire de AlexN le 18/07/2007 22:20:23

Désolé, je ne comprend pas ta question. Si tu lis en haut de la page, tu trouves :

private enum Status { STOPPED, RUNNING, PAUSED };
private Status status = Status.STOPPED;

pour les enum il faut un jdk version quelquechose minimum, je ne sais plus, mais avec la toute dernière version tu devrais pouvoir les utiliser.

Commentaire de danimo le 20/07/2007 00:01:07

Voila ce qu'il se passe lors de la compile :
javac ButtonBlinker.java
ButtonBlinker.java:22: ';' expected
      private enum Status { STOPPED, RUNNING, PAUSED };
                          ^
ButtonBlinker.java:22: cannot resolve symbol
symbol  : class enum
location: class ButtonBlinker
      private enum Status { STOPPED, RUNNING, PAUSED };
              ^
ButtonBlinker.java:23: cannot resolve symbol
symbol  : class Status
location: class ButtonBlinker
      private Status status = Status.STOPPED;
              ^
ButtonBlinker.java:139: cannot resolve symbol
symbol  : class Status
location: class ButtonBlinker
      private Status switchSuspended()
              ^
ButtonBlinker.java:143: cannot resolve symbol
symbol  : class Status
location: class ButtonBlinker
      public void updateCommandButtons(Status status)
                                       ^
ButtonBlinker.java:147: cannot resolve symbol
symbol  : variable RUNNING
location: class ButtonBlinker
            case RUNNING: startButton.setEnabled(false);
                 ^
ButtonBlinker.java:153: cannot resolve symbol
symbol  : variable PAUSED
location: class ButtonBlinker
            case PAUSED: startButton.setEnabled(false);
                 ^
ButtonBlinker.java:158: cannot resolve symbol
symbol  : variable STOPPED
location: class ButtonBlinker
            case STOPPED: startButton.setEnabled(true);
                 ^
8 errors

et je crois qu'effectivement je n'ai pas le bon jdk.

Merci tout de meme !

Commentaire de AlexN le 20/07/2007 18:10:37

jdk 5.0 minimum pour avoir les enum

Commentaire de papillon2000 le 29/03/2009 18:23:46

bonjour à tous, pour moi je veux utiliser la classe timer pour contrôler l'exécution d'un ensemble de taches. telque à la terminaison de la tache1 je dois déclancher la tache 2 en controlant la fin du temps d'exécution de la tache1 mais avec un temps réel(horloge)

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

timer exécuté par un bouton [ par stickasia ] salutje débute en JAVA et j'aimerai en appuyant sur un bouton déplacer un carré de gauche a droite avec un timer dans une fenetre et c avec SWING qq1 problème d'affichage d'éléments en swing [ par jc_romeo ] BonjourJe suis en train de développer un programme en swing et il faut à un moment que je puisse rajouter des boutons au fur et à mesure que l'on séle faire clignoter un bouton [ par niko29940242 ] salut allj'ai un pti pb, j'essaie de faire clignoter un Jbutton jusqu'a ce qu'il y ait un click de l'utilisateur masi ca marche povoici le code : int Liens entre javax.swing et java.awt ??? [ par Listener ] Bonjour,Depuis quelques temps, j'essaie de me familiariser avec les classes de la bibliothèque Swing et celles de l'Abstract Windowing Toolkit. E Help TIMER swing [ par mimilavitrine ] BOnjour a tous, j'ai un peu de mal a comprendre le timer j'aurais une question: Dans cette declaration de TIMER : import javax.swing.Timer; publi fenetre swing [ par alonsyl ] bonjour,je cherche a construire une fenetre avec SWING.je me pose tout un tas de questions a chacune des etapes de conception :1) "setBounds(x, y, 400 Swing bouton parcourir [ par ultrafil ] Bonjour, J'aimerai implémenter un bouton "browse", pour selectionner un fichier sur disque. Malheureusement, je ne trouve pas le nom de la clas Récuper nom d'un bouton (DEBUTANT) [ par MATHIS49 ] Bonjour , J'ai plusieur écouteurs sur plusieurs boutons qui sont sur une interface graphique. J'aimerais savoir comment récupérer le nom du bouton q Swing (passage à une autre fenêtere on click sur Bouton)! [ par mnasri_riadh ] Salut tout le monde,Je suis débutant en java Swing, je veux faire une petite interface swing qui suite au click sur un bouton, elle m'amène Problème Timer (Swing) JAVA [ par kevvvv ] Bonjour, j'ai un problème avec les Timer (Swing) de Java. En fait, je dois réaliser un projet d'école et mon thème est Super Mario Bros Tout fonctionn


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,796 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales