titulo dinĂ¡mico en barra de un JFrame con Timer y TimerTask

Technology

titulo dinĂ¡mico en barra de un JFrame con Timer y TimerTask

aqui les dejo un codigo donde podemos mover el titulo con estilo banner a lo largo de la barra en un jframe en java vamos a utilizar Timer y TimerTask


primero creamos una Jframe y luego importamos las siguientes librerias:




import java.util.Timer;
import java.util.TimerTask;
luego declaramos 4 variables

 private Timer tiempo;
      private TimerTask task;
          private int indice = 0;
           private int speed = 80;
luego ponemos los siguientes metodos:

  public String texto() {
        return "mensaje en la barra...";
    }

    void settexto(int i) {
        this.setTitle(texto().substring(0, i));
    }

    public void startAnimation() {
        tiempo = new Timer();
        task = new TimerTask() {

            public void run() {
                if (indice == texto().length()) {
                    indice = 1;
                } else {
                    indice++;
                }
                settexto(indice);
            }
        };
        tiempo.schedule(task, 0, speed);
    }
por ultimo llamos al metodo startAnimation() desde el construtor por defecto por ejemplo si tenemos que el iframe lo llamos vista podemos llamar al metodo asi:

  public vista() {
        initComponents();
        startAnimation();
    }
y eso es todo al final quedara asi:


Post a Comment

0 Comments