Results 1 to 2 of 2
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie]

    I'm trying to implement an inner class, however, I'm a bit stuck...
    :confused:

    Java Code:
    TalkingClock.java
    package homenetwork.bkr.training;
    
    import java.awt.Toolkit;
    import java.awt.event.*; //required for ActionListener
    import java.util.Date;
    
    import javax.swing.Timer;   
    
    public class TalkingClock {
    	/** A talking clock
    	 * @param interval: the interval between messages (milliseconds)
    	 * @param beep: true if the clock should beep 
    	 */
    	public TalkingClock (int interval, boolean beep)
    	{
    		this.interval = interval;
    		this.beep = beep;
    	}
    	
    	/** 
    	 * Starts the clock
    	 */
    	public void start() {
    		ActionListener listener = new TimePrinter(); //error: ActionListener cannot be resolved
    		Timer t = new Timer(interval, listener);
    		t.start();
    	}
    	
    	//the inner class
    	public class TimePrinter implements ActionListener {
    		
    		public void actionPerformed (ActionEvent event)
    		{
    			Date now = new Date();
    			System.out.println("At the tone, the time is: " + now);
    			if (beep) Toolkit.getDefaultToolkit().beep();
    			
    		}
    	}
    	
    	private int interval; //this is interval
    	private boolean beep; //this is beep
    } 
    
    
    
    Test.java
    package homenetwork.bkr.training;
    
    import javax.swing.JOptionPane;
    
    public class Test {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		TalkingClock clock = new TalkingClock(1000,true);
    		clock.start();
    		
    		//keep program running until user selects "OK"
    		JOptionPane.showMessageDialog(null, "Quit Program?");
    		System.exit(0);
    	}
    
    }
    Compilation errors:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    interval cannot be resolved or is not a field
    beep cannot be resolved or is not a field
    interval cannot be resolved
    Syntax error, insert "}" to complete ClassBody

    at homenetwork.bkr.training.TalkingClock.<init>(Talki ngClock.java:16)
    at homenetwork.bkr.training.Test.main(Test.java:11)
    Last edited by jon80; 05-20-2009 at 07:29 PM. Reason: update #3

  2. #2
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

Similar Threads

  1. Another newbie
    By PhHein in forum Introductions
    Replies: 0
    Last Post: 04-22-2009, 02:26 PM
  2. Newbie Help
    By mattkid in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 05:55 AM
  3. I am newbie
    By Seoplanner in forum Introductions
    Replies: 0
    Last Post: 11-11-2008, 02:22 PM
  4. :) newbie...........
    By Somitesh Chakraborty in forum Introductions
    Replies: 1
    Last Post: 08-19-2008, 10:00 AM
  5. newbie newbie newbie
    By krislogy in forum New To Java
    Replies: 9
    Last Post: 08-15-2008, 01:28 AM

Posting Permissions

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