DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    kalamazoo lou Guest

    Why won't this work?


    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.

    thnx

    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

    {
    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();
    Label TimeIs=new Label("The Current Time is ");
    Label RightNow=new Label(TimeNow.getTime());
    add(TimeIs);
    add(RightNow);
    }
    }


  2. #2
    atl-rus Guest

    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);
    >}
    > }
    >



  3. #3
    Panos Guest

    Re: Why won't this work?


    // THIS CODE WORKS MUCH BETTER

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    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() );
    }

    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links