Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By gimbal2

Thread: Minutes Class

  1. #1
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Exclamation Minutes Class

    I have this code for a Countdown Timer...
    Java Code:
    package javaproject;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import Countdown.Timer;
    import java.util.Timer;
    import javaproject.Countdown;
    
    public class Minutes {
    	int count = 30; //Integer count is set equal to 30.
    	int delay = 1000; // Integer delay is equal to 1000. 1000 being in milliseconds which is 1 second.
    
    	ActionListener actionToPreform = new ActionListener() {  //First we create the new action listener object.
    	      public void actionPerformed(ActionEvent evt) { //Then we create the method that is called each time the action listener is called.
    	          //...Perform your task here, whatever it may be. In this case its decreasing our count by 1.
    	        if(count > 0){
    	            count--;  //Decrease our count.
    	        }
    
    
    
    	      new Timer(30).start(1000);// Create the new timer. First paramter is the delay of the timer, the second paramater is the action listener it needs to call. Finally we start the Timer running by using it's .start() method.
    
    
    	}
    	};
    }
    It said I have to setup a Countdown Class / Package

    here is that code
    Java Code:
    package javaproject;
    
    public class Countdown {
        public static void main(String args[]){
            
        }
    
    }
    Do I need to put more code in the Countdown class or in the Minutes Class

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Minutes Class

    That is not what you want to ask. You must have already compiled and executed this and seen that nothing at all happens, so you KNOW that you're not done yet.

    Or you haven't compiled and run this code yet, in which case you're asking questions before you know what you have to ask.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Default Re: Minutes Class

    What code could I possibly put in...

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Minutes Class

    The correct code, after you think about it and perhaps read through your Java book some more. I presume this is your code so you know what it is supposed to do and thus can identify what it isn't doing yet :/

    I'll give you a hint: that Countdown class does exactly nothing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Default Re: Minutes Class

    So It's an empty class...

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Minutes Class

    SEX WITH A ZEBRA!

    Now that I have got your attention: like I said in the previous thread about this, and I am going to say this again: DO NOT PUT THE TIMER VARIABLE INSIDE THE ACTIONLISTENER! I have use my CapsLock powers to make you hear me scream. Re-read the thread I linked (it's your thread) and see what went wrong.

    Then a confusing issue: there are two classes called Timer. One from the java.util package and on in the Countdown package (note: the Java naming conventions dictate to write packages all lowercase). The Timer object on line 23, which one is it? Apparently it's your home-brew Timer, since the java.util.Timer doens't have a constructor Timer(int).

    Then there is this:
    Java Code:
    new Timer(30).start(1000);// Create the new timer. First paramter is the delay of the timer, the second paramater is the action listener it needs to call. Finally we start the Timer running by using it's .start() method.
    Where do you tell the Timer to use your ActionListener? That's right. You don't. The Timer does not know automagically which ActionListener to use, even though you create it inside the ActionListener.

    So what to do now? First read all my replies in the other thread, linked here for your convenience: http://www.java-forums.org/new-java/...s-program.html.
    After you're done there, come back here, slap yourself in the face and shout: I WILL NOT PUT THE TIMER VARIABLE INSIDE THE ACTIONLISTENER!

    I will go for a coffee now.
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Default Re: Minutes Class

    I've come up with this code now, surely I shouldn't have to create so many classes...

    Java Code:
    package javaproject;
    
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.util.Timer;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
             public class CountDown{
          //super( "Timer Example" );
    
          String names[] =
             { "0.gif", "1.gif", "2.gif", "3.gif", "4.gif",
               "5.gif", "6.gif", "7.gif", "8.gif", "9.gif" };
    
          // create and add icons
          iconArray = new Icon (names.length ) {
    
            public void paintIcon(Component c, Graphics g, int x, int y) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            public int getIconWidth() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            public int getIconHeight() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };
    
          for ( int count = 0; count < names.length; count++ )
            {
             iconArray[ count ] = new ImageIcon ( names[ count ] );
            }
    
    
          int delay = 1000;
          timeClock = new Timer(delay, new TimerHandler () );
          timeClock2 = new Timer(delay, new TimerHandler2 () );
    
          // get content pane and set its layout
          Container container = getContentPane();
          container.setLayout( new FlowLayout() );
    
          // create buttons
          plainButton = new JButton( "Show Delayed Message" );
          container.add( plainButton );
    
    
          JButton = new JButton( "Click to Count Down", iconArray[9] );
    
          container.add( fancyButton );
    
          // create an instance of inner class ButtonHandler
          // to use for button event handling
          ButtonHandler handler = new ButtonHandler();
          ButtonHandler1 handler1 = new ButtonHandler1();
          ButtonHandler2 handler2 = new ButtonHandler2();
          fancyButton.addActionListener( handler2 );
          plainButton.addActionListener( handler1 );
    
          setSize( 275, 175 );
          setVisible( true );
    
        private Container getContentPane() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
       } // end CountDown constructor

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Minutes Class

    Are you writing this in Notepad without a JDK or something? When you run javac, you would get this:
    Java Code:
    CountDown.java:19: error: <identifier> expected
        iconArray=new Icon(names.length) {
                 ^
    CountDown.java:34: error: illegal start of type
    for(int count=0;count<names.length;count++)
    ^
    CountDown.java:34: error: ')' expected
    for(int count=0;count<names.length;count++)
                 ^
    CountDown.java:34: error: illegal start of type
    for(int count=0;count<names.length;count++)
                  ^
    CountDown.java:34: error: <identifier> expected
    for(int count=0;count<names.length;count++)
                   ^
    CountDown.java:34: error: ';' expected
    for(int count=0;count<names.length;count++)
                    ^
    CountDown.java:34: error: > expected
    for(int count=0;count<names.length;count++)
                               ^
    CountDown.java:34: error: '(' expected
    for(int count=0;count<names.length;count++)
                                      ^
    CountDown.java:34: error: <identifier> expected
    for(int count=0;count<names.length;count++)
                                            ^
    CountDown.java:34: error: illegal start of type
    for(int count=0;count<names.length;count++)
                                              ^
    CountDown.java:34: error: <identifier> expected
    for(int count=0;count<names.length;count++)
                                               ^
    CountDown.java:35: error: ';' expected
            {
             ^
    CountDown.java:36: error: illegal start of type
            iconArray[count]=new ImageIcon(names[count]);
                     ^
    CountDown.java:36: error: ';' expected
            iconArray[count]=new ImageIcon(names[count]);
                           ^
    CountDown.java:36: error: invalid method declaration; return type required
            iconArray[count]=new ImageIcon(names[count]);
                                 ^
    CountDown.java:36: error: ']' expected
            iconArray[count]=new ImageIcon(names[count]);
                                                 ^
    CountDown.java:36: error: ')' expected
            iconArray[count]=new ImageIcon(names[count]);
                                                      ^
    CountDown.java:36: error: illegal start of type
            iconArray[count]=new ImageIcon(names[count]);
                                                       ^
    CountDown.java:36: error: <identifier> expected
            iconArray[count]=new ImageIcon(names[count]);
                                                        ^
    CountDown.java:36: error: ';' expected
            iconArray[count]=new ImageIcon(names[count]);
                                                         ^
    CountDown.java:41: error: <identifier> expected
            timeClock=new Timer(delay,new TimerHandler());
                     ^
    CountDown.java:42: error: <identifier> expected
            timeClock2=new Timer(delay,new TimerHandler2());
                      ^
    CountDown.java:46: error: <identifier> expected
            container.setLayout(new FlowLayout());
                               ^
    CountDown.java:46: error: illegal start of type
            container.setLayout(new FlowLayout());
                                ^
    CountDown.java:46: error: ')' expected
            container.setLayout(new FlowLayout());
                                   ^
    CountDown.java:46: error: ';' expected
            container.setLayout(new FlowLayout());
                                              ^
    CountDown.java:46: error: illegal start of type
            container.setLayout(new FlowLayout());
                                               ^
    CountDown.java:46: error: <identifier> expected
            container.setLayout(new FlowLayout());
                                                ^
    CountDown.java:49: error: <identifier> expected
            plainButton=new JButton("Show Delayed Message");
                       ^
    CountDown.java:50: error: <identifier> expected
            container.add(plainButton);
                         ^
    CountDown.java:50: error: <identifier> expected
            container.add(plainButton);
                                     ^
    CountDown.java:53: error: <identifier> expected
            JButton=new JButton("Click to Count Down",iconArray[9]);
                   ^
    CountDown.java:55: error: <identifier> expected
            container.add(fancyButton);
                         ^
    CountDown.java:55: error: <identifier> expected
            container.add(fancyButton);
                                     ^
    CountDown.java:62: error: <identifier> expected
            fancyButton.addActionListener(handler2);
                                         ^
    CountDown.java:62: error: <identifier> expected
            fancyButton.addActionListener(handler2);
                                                  ^
    CountDown.java:63: error: <identifier> expected
            plainButton.addActionListener(handler1);
                                         ^
    CountDown.java:63: error: <identifier> expected
            plainButton.addActionListener(handler1);
                                                  ^
    CountDown.java:65: error: invalid method declaration; return type required
            setSize(275,175);
            ^
    CountDown.java:65: error: illegal start of type
            setSize(275,175);
                    ^
    CountDown.java:65: error: illegal start of type
            setSize(275,175);
                        ^
    CountDown.java:66: error: invalid method declaration; return type required
            setVisible(true);
            ^
    CountDown.java:66: error: illegal start of type
            setVisible(true);
                       ^
    43 errors
    When you see a boatload of errors like this, first and foremost check your braces. The curly ones {}, the straight ones [], the round ones (). After you verified that, compile again.

    I have feeling you bite off more than you can chew. First try to write a proper class. You know, public class SomeName. Get your curly braces right. Make sure it compiles. Then add a constructor. Get your curly braces right. Make sure it compiles. Then add a method. Get your curly braces right. Make sure it compiles. Don't even worry about mysterious inner classes like the dreaded ActionListener, until you fully understand what you're doing. And again, get your curly braces right. Make sure it compiles. Did I mention to check your curly braces?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Default Re: Minutes Class

    Ha! yeah you did. Will do... Shouldn't need much classes though...

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Minutes Class

    What do you mean with "much classes" [sic]? Apart from the wonky code in your example, all Swing code (sort of) looks like this. I would recommend focussing on one part and one part only. Make your Minutes class work first. Practice Timers and ActionListeners in a little class to play with. Once you understand how to create Timers with their ActionListeners, implement it in the big picture.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    aboyse1990 is offline Member
    Join Date
    Nov 2014
    Posts
    61
    Rep Power
    0

    Default Re: Minutes Class

    Much classes, it's asking me to implement a lot of classes to get it working. But, it could end up going horribly wrong...

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Minutes Class

    Quote Originally Posted by SurfMan View Post
    SEX WITH A ZEBRA!
    That is oddly specific.

    Have fun with this thread dude, its my turn to chew on some popcorn.
    aboyse1990 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Minutes Class

    Quote Originally Posted by aboyse1990 View Post
    Much classes, it's asking me to implement a lot of classes to get it working. But, it could end up going horribly wrong...
    If you're referring to the number of imports at the top of the code, that's perfectly normal. Otherwise, I have no clue what you mean.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Minutes to return
    By Army in forum New To Java
    Replies: 5
    Last Post: 01-04-2013, 09:42 PM
  2. Call a method every 5 minutes.
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 04:31 AM
  3. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 08:35 AM
  4. How to beep every 5 minutes in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •