Timer

2 respostas
S

Primeiro post aqui.

Boa tarde, estou com um problema para usar Timer e TimerTask.

Eu dei uma pesquisada no google, achei artigos aqui, e inclusive tentei usando esse tuto:
http://www.dsc.ufcg.edu.br/~jacques/cursos/map/html/threads/timer.html

Mas não consigo fazer o que eu quero.

É o seguinte, tenho um programa que chama Senhas. O sujeito digita a senha em um txt no frame1 e ao clicar em “Chamar” um label no fram2 muda o texto para o digitado.

Eu quero fazer o seguinte, quando ele clicar em chamar, além de mudar o texto na label, ele piscasse 3 vezes.

Meu código atual da classe do timer que criei:

package Model;

import java.util.Timer;
import java.util.TimerTask;

import View.Frmexibesenha;

public class Tempo {

    Timer timer;
    Frmexibesenha form = new Frmexibesenha();

    public Tempo(int seconds){
    	timer = new Timer();

    	timer.scheduleAtFixedRate(new RemindTask(), seconds*1000, 1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
           form.lblSenha.setVisible(false);

        }



    }
}

Desde ja, grato.

2 Respostas

E

Veja se isso pisca do jeito que você quer.

package guj;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JLabel;

public class BlinkingLabel extends JLabel {

    private void initialize() {
        hide = false;
        count = 0;
        timer = new javax.swing.Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (hide) {
                    foregroundColor = getForeground();
                    setForeground(getBackground());
                } else {
                    setForeground(foregroundColor);
                }
                hide = !hide;
                count++;
                if (count == times * 2 + 1) {
                    timer.stop();
                    count = 0;
                }
            }
        });

    }

    public BlinkingLabel() {
        super();
        initialize();
    }

    public BlinkingLabel(String text) {
        super(text);
        initialize();
    }

    public BlinkingLabel(Icon image) {
        super(image);
        initialize();
    }

    public BlinkingLabel(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
        initialize();
    }

    public BlinkingLabel(Icon image, int horizontalAlignment) {
        super(image, horizontalAlignment);
        initialize();
    }

    public BlinkingLabel(String text, Icon icon, int horizontalAlignment) {
        super(text, icon, horizontalAlignment);
        initialize();
    }

    public void blink (int times) {
        hide = false;
        count = 0;
        this.times = times; 
        timer.start();
    }
    private javax.swing.Timer timer;
    private boolean hide;
    private int count;
    private int times;
    private Color foregroundColor;
}
package guj;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;

public class ExemploPiscar extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private BlinkingLabel blkTeste = null;
    private JPanel pnlBotoes = null;
    private JButton btnTestar = null;
    public BlinkingLabel getBlkTeste() {
        if (blkTeste == null) {
            blkTeste = new BlinkingLabel();
            blkTeste.setText("Exemplo");
        }
        return blkTeste;
    }
    
    /**
     * This method initializes pnlBotoes	
     * 	
     * @return javax.swing.JPanel	
     */
    private JPanel getPnlBotoes() {
        if (pnlBotoes == null) {
            pnlBotoes = new JPanel();
            pnlBotoes.setLayout(new FlowLayout());
            pnlBotoes.add(getBtnTestar(), null);
        }
        return pnlBotoes;
    }

    /**
     * This method initializes btnTestar	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getBtnTestar() {
        if (btnTestar == null) {
            btnTestar = new JButton();
            btnTestar.setText("Testar");
            btnTestar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    blkTeste.setText (String.format ("Teste %d" , i++));
                    blkTeste.blink(3);
                }
                private int i = 0;
            });
        }
        return btnTestar;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ExemploPiscar thisClass = new ExemploPiscar();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

    /**
     * This is the default constructor
     */
    public ExemploPiscar() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("Exemplo Piscar");
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getBlkTeste(), BorderLayout.NORTH);
            jContentPane.add(getPnlBotoes(), BorderLayout.SOUTH);
        }
        return jContentPane;
    }

}
S

Funcionou perfeitamente…

Mas eu não consigo aplicar isso no meu projeto, acho que é um pouco de mais pro meu parco conhecimento hehehe.

Se você puder me explicar um pouco sobre o código, eu não consegui entender muito bem algumas partes, tais como:

public BlinkingLabel getBlkTeste() { if (blkTeste == null) { blkTeste = new BlinkingLabel(); blkTeste.setText("Exemplo"); } return blkTeste; }

public BlinkingLabel() {   
        super();   
        initialize();   
    }   
  
    public BlinkingLabel(String text) {   
        super(text);   
        initialize();   
    }   
  
    public BlinkingLabel(Icon image) {   
        super(image);   
        initialize();   
    }   
  
    public BlinkingLabel(String text, int horizontalAlignment) {   
        super(text, horizontalAlignment);   
        initialize();   
    }   
  
    public BlinkingLabel(Icon image, int horizontalAlignment) {   
        super(image, horizontalAlignment);   
        initialize();   
    }   
  
    public BlinkingLabel(String text, Icon icon, int horizontalAlignment) {   
        super(text, icon, horizontalAlignment);   
        initialize();   
    }   
  
    public void blink (int times) {   
        hide = false;   
        count = 0;   
        this.times = times;   
        timer.start();   
    }   
    private javax.swing.Timer timer;   
    private boolean hide;   
    private int count;   
    private int times;   
    private Color foregroundColor;   
}

Ficaria agradecido se você pudesse me explicar isso melhor para que eu pudesse entender e aplicar às minhas necessidades.

Grato.

Criado 11 de novembro de 2010
Ultima resposta 11 de nov. de 2010
Respostas 2
Participantes 2