Why

Why won't the following code work to display Date/Time in my applet?

//Push Button for time Applet

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class TodayIs extends Applet

{

private Button pushMe1,pushMe2;

public void init()

{

// Create pushMe1 Button

pushMe1=new Button("Press here for Date");

pushMe1.addActionListener(new pushMe1Handler(this));

add(pushMe1);

// Create pushMe2 Button

pushMe2=new Button("Press here for Time");

pushMe2.addActionListener(new pushMe2Handler(this));

add(pushMe2);

}

}

//Event handler for pushMe1

class pushMe1Handler implements ActionListener

{

Applet applet;

public pushMe1Handler( Applet a) {applet=a;}

public void actionPerformed(ActionEvent e)

{

Date toDay=new Date();

System.out.print("Todays Date is");

System.out.println(toDay);

}

}

//Event handler for pushMe2

class pushMe2Handler implements ActionListener

{

Applet applet;

public pushMe2Handler( Applet a){applet=a;}

public void actionPerformed(ActionEvent e)

{

Date TimeNow=new Date();

System.out.print("The current time is ");

System.out.print(TimeNow.getTime());

}

}

thnx

[1420 byte] By [kalamazoolou] at [2007-9-26 4:13:20]
# 1

Hello,

I think your problem is in how you print your data to the screen.

You probably have to use the drawString method (in the Graphics class.) System.out.print won't display in an applet, since all the text on the applet's canvas is GRAPHICAL text.

To do this you must overide the applet's paint method and do all your modifications to the canvas in there.

I THINK this is right... I wouldn't if someone could confirm (or correct) that for me though :)

Hope this helps. Let me know if it works.

lucretia6 at 2007-6-29 13:19:58 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
You are correct. He displays his information to DOS screen.He must use paint() function to draw to screen.
TQnguyen at 2007-6-29 13:19:58 > top of Java-index,Archived Forums,New To Java Technology Archive...