I'm a total newbie, trying to teach myself Java and can't seem to figure out
why this won't work. I'm thinking it has something to do with it being an
applet, but I haven't a clue as to what I have done wrong here. Any help
would be greatly appreciated.
btw- the intent is to have two buttons, one that displays the current date
and the other, the current time.
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();
Label TimeIs=new Label("The Current Time is ");
Label RightNow=new Label(TimeNow.getTime());
add(TimeIs);
add(RightNow);
}
}
08-16-2001, 09:15 PM
atl-rus
Re: Why won't this work?
Try like that:
>
>heres the code:
>//Push Button for time Applet
>
>import java.applet.Applet;
>import java.awt.*;
>import java.awt.event.*;
>import java.util.*;
>
>public class TodayIs extends Applet
>
> {
> public 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();
> Label TimeIs=new Label("The Current Time is ");
> Label RightNow=new Label(TimeNow.getTime());
> add(TimeIs);
> add(RightNow);
>}
> }
>
public class TodayIs extends Applet implements ActionListener
{
public Button pushMe1,pushMe2;
public Label ToDay,rightNow;
public Date timeNow,toDay;
public Panel pane;
public void init()
{
pushMe1=new Button("Press here for Date");
pushMe1.addActionListener(this);
pushMe2=new Button("Press here for Time");
pushMe2.addActionListener(this);
timeNow=new Date();
toDay=new Date();
ToDay=new Label("Todays Date is ");
rightNow=new Label("The Current Time is ");
pane=new Panel();
pane.setLayout(new GridLayout(2,2));
pane.add(pushMe1);
pane.add(ToDay);
pane.add(pushMe2);
pane.add(rightNow);
add(pane);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==pushMe1)
ToDay.setText("" +toDay );
else
rightNow.setText(""+ timeNow.getTime() );
}