1 Replies - 2698 Views - Last Post: 05 April 2011 - 12:16 AM Rate Topic: -----

#1 lostinclass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 18-March 11

uses timer to print the current time

Posted 04 April 2011 - 11:11 PM


[b]i added date now to my code but nothing shows up. in my actionlistener. what am i doing wrong[/b]
/**
   Displays the current time once every second.
*/
public class TimePrinter
{
   
	
	public static void main(String[] args)
   {
      class CurrentTime implements ActionListener
      {
        Date now = new Date();
        System.out.println(now);
      }

   

      final int DELAY = 1000; // milliseconds between timer ticks
      Timer t = new Timer(DELAY, . . .);
      t.start();

      JOptionPane.showMessageDialog(null, "Quit?");
      System.exit(0);
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: uses timer to print the current time

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: uses timer to print the current time

Posted 05 April 2011 - 12:16 AM

Kind of little bit mixed up ?
Where is your actionPerformed() ?
What object do you pass to your Timer constructor ?
This should do it.


/**
   Displays the current time once every second.
*/
import java.awt.event.*;
import java.util.Date;
import java.util.Scanner;

import javax.swing.*;
public class TimePrinter implements ActionListener
{
    final int DELAY = 1000; // milliseconds between timer ticks
   
	TimePrinter () {
	    Timer t = new Timer(DELAY, this);
	    t.start();
	}
	
	public void actionPerformed(ActionEvent e) {
        Date now = new Date(System.currentTimeMillis());
        System.out.println(now);		
	}
	
	public static void main(String[] args)
    {
		new TimePrinter();
		Scanner scan = new Scanner(System.in);
		scan.nextLine();
		System.exit(0);
    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1