• Lunes 29 de Abril de 2024, 00:26

Autor Tema:  Desplazar Un Texto Por Un Jlabel, Jbutton O Más  (Leído 1600 veces)

jrariasf

  • Nuevo Miembro
  • *
  • Mensajes: 5
    • Ver Perfil
Desplazar Un Texto Por Un Jlabel, Jbutton O Más
« en: Miércoles 28 de Noviembre de 2007, 09:07 »
0
Hola,
Quiero hacer que un texto aparezca sobre un JLabel o un JButton (o sobre otro componente).
La longitud del texto es mayor que el tamaño del JLabel con lo que me gustaría saber si existe alguna forma de hacer que ese texto vaya apareciendo y se vaya desplazando poco a poco de forma que vea todo su contenido y cuando finalice vuelva a aparecer de nuevo el texto.... es decir, una especie de "scroll" automático.... similar a los típicos carteles-anuncios donde va apareciendo un mensaje..

No sé si Java tiene ya algún componente que haga esto o alguien conoce el código fuente necesario.....

Muchas gracias y un saludo.

jrariasf

  • Nuevo Miembro
  • *
  • Mensajes: 5
    • Ver Perfil
Re: Desplazar Un Texto Por Un Jlabel, Jbutton O Más
« Respuesta #1 en: Jueves 29 de Noviembre de 2007, 16:31 »
0
Me contesto a mi mismo:
Al final lo que he hecho ha sido crearme una clase derivada de un JLabel.... de forma que se lanza un thread que va cambiando el texto del JLabel y de la impresión de que va desplazándose:

Código: Text
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4.  
  5. class Anuncio extends JLabel
  6. {
  7.   String texto;
  8.   String link;
  9.  
  10.   public Anuncio(String _texto)
  11.   {
  12.      this(_texto, "");
  13.   }
  14.  
  15.   public Anuncio(String _texto, String _link)
  16.   {
  17.      super(_texto);
  18.      texto = _texto;
  19.      link = _link;
  20.      setForeground(Color.white);
  21.      setPreferredSize(new Dimension (120, 30));
  22.      ControlAnuncio controlador = new ControlAnuncio(this);
  23.      controlador.start();    
  24.   }
  25. }
  26.  
  27. class ControlAnuncio extends Thread
  28. {
  29.   Anuncio anuncio;
  30.  
  31.   public ControlAnuncio(Anuncio _anuncio)
  32.   {
  33.      anuncio = _anuncio;
  34.   }
  35.  
  36.   public void run()
  37.   {
  38.      int tamTotal = anuncio.texto.length();
  39.      byte[] letras = anuncio.texto.getBytes();
  40.      int inicio=0;
  41.      String cabecera, cola;
  42.      
  43.      do {    
  44.        try {
  45.           sleep(300);
  46.        } catch(InterruptedException ie) {
  47.           System.err.println("Capturada InterruptedException en run de Anuncio: "+ie);
  48.        }
  49.        cabecera = new String(letras, inicio, tamTotal-inicio);
  50.        cola = new String(letras, 0, inicio);
  51.        anuncio.setText(cabecera + cola);
  52.        
  53.        inicio++;
  54.        if (inicio > tamTotal)
  55.        {
  56.           inicio = 0;
  57.           try {
  58.              sleep(4000);
  59.           } catch(InterruptedException ie) {
  60.              System.err.println("Capturada InterruptedException en run de Anuncio: "+ie);
  61.           }
  62.         }
  63.      }while (true);
  64.    
  65.   }
  66.  
  67. }
  68.  
  69.