• Sábado 21 de Septiembre de 2024, 19:39

Autor Tema:  Ambiguedad Con Timers  (Leído 1640 veces)

Blizknight

  • Miembro activo
  • **
  • Mensajes: 41
    • Ver Perfil
Ambiguedad Con Timers
« en: Lunes 31 de Julio de 2006, 16:53 »
0
Hola como estais? el problema que tenia era de ambiguedad con el timer, creo que seria mejor que lo compilen y lo vean, explicar eso no se muxo


Código: Text
  1. /*
  2. * Reloj.java
  3. */
  4.  
  5. /**
  6. *
  7. */
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import java.util.Date;
  12. import java.text.*;
  13. import java.util.*; //agregue esta linea para lo del Gregorian calendar
  14.  
  15. public class Reloj extends JFrame
  16. {
  17.  
  18. Date hora, fecha,f1,f2;
  19. String patrón, mod;
  20. JLabel jlbHoraActual, lbl;
  21. JButton btn;
  22. ActionListener al;
  23. Timer timer1, timer2;  
  24. SimpleDateFormat formato, format2;
  25.  
  26.  public Reloj()
  27.  {
  28.    jlbHoraActual = new JLabel();
  29.    getContentPane().setLayout(null);
  30.    setResizable(false);
  31.    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  32.    //jlbHoraActual.setFont(new Font("Arial", 1, 24));
  33.    jlbHoraActual.setHorizontalAlignment(SwingConstants.CENTER);
  34.    jlbHoraActual.setText("00:00:00");
  35.    getContentPane().add(jlbHoraActual);
  36.    jlbHoraActual.setBounds(40, 90, 210, 40);
  37.    
  38.    setSize(290, 260);
  39.    
  40.    lbl = new JLabel("");
  41.    lbl.setBounds(40, 130, 210, 40);
  42.    getContentPane().add(lbl);
  43.    lbl.setBackground(Color.red);
  44.    lbl.setOpaque(true);
  45.    
  46.    btn = new JButton("Time");
  47.    btn.setBounds(40, 170, 210, 40);
  48.    getContentPane().add(btn);
  49.    
  50.    
  51.    
  52.  
  53.  
  54.     al = new ActionListener()
  55.    {
  56.      public void actionPerformed(ActionEvent e)
  57.     {
  58.    
  59.        onTimer();
  60.      }
  61.    };
  62.    // Crear un temporizador e iniciarlo
  63.    timer1 = new Timer(1000, al);
  64.    timer1.start();
  65.  
  66.    GregorianCalendar g = new GregorianCalendar();
  67.   long horas = g.get(GregorianCalendar.HOUR_OF_DAY);
  68.   long mins = g.get(GregorianCalendar.MINUTE);
  69.    
  70.    lbl.setText(horas + mins);
  71.   //hasta aqui se inserto lo nuevo
  72.   /* timer2 = new Timer(1000, al);
  73.    timer2.start();*/
  74.    
  75.   }
  76.  
  77.  
  78.  private void onTimer()
  79.  {
  80.    hora = new Date();
  81.    patrón = "dd' de 'MMMM' del 'yy' 'hh:mm:ss";//dd:hh:mm:ss yy/mm/dd  dd.MMMMM.yy
  82.    
  83.  
  84.    
  85.    formato = new SimpleDateFormat(patrón);
  86.    jlbHoraActual.setText(formato.format(hora));
  87.    
  88.    
  89.    
  90.    /*fecha = new Date();
  91.    mod  = "dd' de 'MMMM' del 'yyyy ";
  92.    
  93.    format2 = new SimpleDateFormat(mod);
  94.    lbl.setText(format2.format(fecha));*/
  95.  }
  96.  
  97.  
  98.  
  99.  
  100.  public static void main(String args[])
  101.  {
  102.    new Reloj().setVisible(true);
  103.  }
  104.  
  105. }
  106.  
  107.  



saludos

jlsoriam

  • Nuevo Miembro
  • *
  • Mensajes: 19
    • Ver Perfil
Re: Ambiguedad Con Timers
« Respuesta #1 en: Martes 1 de Agosto de 2006, 01:38 »
0
Por eso no es bueno usar cargar todas las clases de un paquete usando el asterisco *, es mejor especificar cada clase que estamos usando.

Algunos IDEs como Netbeans brindan esa funcionalidad (opcion FixImports) que automaticamente llena los imports de todas las clases que esta utilizando.

En tu programa, esta es la cabecera de los imports.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.WindowConstants;

Y tambien, a la ultima linea del constructor la modifique asi:
lbl.setText(String.valueOf(horas + mins));
ya que no me compilaba.

Con estas modificaciones ya compilaba y ejecutaba.
Saludos

José